8

I’d like to render a 2D chessboard image and save it after the last move as a PNG or JPEG from a given PGN file on a Linux console. How can I do this?

I have a PGN file and I want to get a screenshot of the board.

Seems to be a simple task but I can’t find a solution for it which is scriptable on the Linux console.

P.S. The wording "simple task" seems to be annoying for some readers here. Being not a native speaker I have a hard time to understand this nuance but I by no mean want to step on anybody's feet. Coming from a Linux console background I have the feeling that this should be doable on the console with ease. But that I haven't found the tool to do it.

  • Not quite sure what you mean by "render" but if you just mean read the file and show the game scid <filename> or xboard <filename> will do it - once you have installed the appropriate one – Ian Bush Mar 15 at 8:08
  • I updated the question. – wintermeyer Mar 15 at 8:25
  • Probably simplest is to open the pgn with your favourite pgn reader and get a screenshot - itsfoss.com/take-screenshot-linux I know of no dedicated tool to do all this in one step, I'll leave the answers for someone that does – Ian Bush Mar 15 at 8:55
  • 1
    PGNs are for storing entire games, so do you want to save an image of the game after the last move? or at a given move? Otherwise, what you probably want to work with are FENs, which is a standard notation for describing a particular position not game. – Ellie Mar 15 at 15:58
  • Hi. I see your edits. Yes it's simple but you still need something like Python package to parse PGN into FENs. PGN by itself is a game, so no unix tool can convert it to an image. – SmallChess Mar 15 at 16:28
17

You can use the SVG rendering functionalities of python-chess. This will allow you to create a SVG-formatted visualisation of any board position loaded using python-chess.

FEN to SVG

For example, given a FEN as input (say we save our python script as boardtosvg.py)

Using python3 and the python-chess library version 0.30.1

import chess
import chess.svg
import sys

arguments = sys.argv
fen = str(arguments[1])

board = chess.Board(fen)
boardsvg = chess.svg.board(board=board)
f = open("BoardVisualisedFromFEN.SVG", "w")
f.write(boardsvg)
f.close()
  • Example, FEN: 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1'

  • Command to run your script from the terminal:

    python boardtosvg.py 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1'

Saved file (in case you cannot open SVGs, you can at least use a browser such as chrome to view the file):

enter image description here

Position from a PGN to SVG

Similarly, if the input is a PGN file, then here's how you'd for istance save the SVG of the last position of the game (you can extend this to saving the position at any move):

import chess
import chess.svg
import chess.pgn
import sys

arguments = sys.argv
pgnfilename = str(arguments[1])

#Read pgn file:
with open(pgnfilename) as f:
    game = chess.pgn.read_game(f)
#Go to the end of the game and create a chess.Board() from it:
game = game.end()
board = game.board()
boardsvg = chess.svg.board(board=board)
f = open("BoardVisualisedFromPGN.SVG", "w")
f.write(boardsvg)
f.close()
  • Example, PGN of Adams vs Kasparov 2005: adams_kasparov_2005.pgn
  • python boardtosvg.py 'adams_kasparov_2005.pgn'

Saved file:

enter image description here

Now all that is left to do is to convert the so-created SVG files to PNG, which you can also do in python, see the solutions in the link below:

| improve this answer | |
8

Yes, of course it's possible. I've done it a lot professionally. However, there is no tool that just takes a PGN chess game and convert it into images. You will need to do some programming, not very hard.

Here is a link on how one can generate machine learning data set on chess. You should be able to reuse the code. I highlighted the part that will relate to your problem.

https://github.com/Elucidation/tensorflow_chessbot has an example:

enter image description here

General steps:

  • Parse your PGN file with Python python-chess. Very easy, trust me.
  • Use your parsed FENs. Follow the workflow above.
| improve this answer | |
0

I don't think calling it a "simple task" in order for someone to give you the code for free is very polite imo.

On top of that, is surely doable, but its not a simple task. It requires implementing a class to handle the graphic representation, and another class to do the "translation". Also there are quite a couple of factors to take into consideration. If you don't have the knowledge to even start to do it, you should hire someone to do it for you then. Afaik, this forum is for helping coders who are actually doing the work, and are stuck in a specific part of their project; its not a codewish fountain for everyone to ask for free implementations.

| improve this answer | |
  • 3
    I don't read this as asking anybody to write code - more if there is something pre-existing already that the OP can install/download. In the Linux world there are bazillions of file format converters available for free, it's not unreasonable to ask if there's one already available. – Ian Bush Mar 15 at 9:30
  • @Jorge Acuna, I do not use Linux, but this IS simple in Windows. You can just use any ChessBase product, including the free viewer, and open the PGN file, get to the position, and go to the "Home" tab, and click "Copy Position". No code involved. Your post is also not an answer, and should have been put in the comments only. – PhishMaster Mar 15 at 9:57
  • Well, it's a simple task. I did it in like 2 hours. The problem described here is like the basics in chess programming. – SmallChess Mar 15 at 15:57
  • Well, doing it from scratch, if you can do that in 2 hours you must be gainfully employed at least. If you have systems in place already, may turn to be a rather fast feature to implement. The original post was not referring to a "tool and/or plugin to extend or create the feature on a Linux console", to my recollection. But, if this is the case and on Linux you do these plugins or scripts and pass them around for free, ok. Forget I mentioned anything. Linux is free for all land no? so I understand you asking for this easily coded tool, according to smartchess. – Jorge Acuna Mar 17 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.