#!/usr/bin/env python # Plot a PGN game using python-chess and ImageMagick, highlighting last moves import os import chess.pgn import chess.svg pgn = open('game.pgn') game = chess.pgn.read_game(pgn) game = game.next() board = game.board() if input("Whose point of view? ('b' for black, nothing for white)\n") == 'b': pov = chess.BLACK else: pov = chess.WHITE delay = input("Delay per move, in centiseconds? (default is 150)\n") if delay == '': delay = "150" start = input("Starting delay? (default is 1)\n") if start == '': start = "1" end = input("End delay? (default is 3)\n") if end == '': end = "3" out = input("Output file name? ('.gif' is suffixed automatically, default is 'pgn.py')\n") if out == '': out = "pgn_py" def svg(i): s = chess.svg.board(board, lastmove = board.peek(), orientation = pov, coordinates = False, size = 800) f1 = "pgn_py_temp_%04u.svg" % i f2 = "pgn_py_temp_%04u.png" % i with open(f1, 'w') as f: f.write(s) os.system("magick %s %s" % (f1, f2)) os.system("rm %s" % f1) i = 0 #try to find it! lm = board.peek() while i < int(start): s = chess.svg.board(board, lastmove = lm, orientation = pov, coordinates = False, size = 800) f1 = "pgn_py_temp_%04u.svg" % i f2 = "pgn_py_temp_%04u.png" % i with open(f1, 'w') as f: f.write(s) os.system("magick %s %s" % (f1, f2)) os.system("rm %s" % f1) i += 1 for m in game.mainline_moves(): board.push(m) svg(i) i += 1 # pause animation at the end: for j in range(int(end)): svg(i) i += 1 # create animation os.system("magick -delay " + delay + " pgn_py_temp_*.png -loop 0 " + out + ".gif") #please don't run this script if you use this naming scheme for family photos os.system("rm pgn_py_temp_*") # ~/ Input \~ # Whose point of view? ('b' for black, nothing for white) # # Delay per move, in centiseconds? (default is 150) # # Starting delay? (default is 1) # 10 # End delay? (default is 3) # # Output file name? ('.gif' is suffixed automatically, default is 'pgn_py') #