#!/usr/bin/env python3
# Plot a PGN game using python-chess and ImageMagick
import os
import chess.pgn
import chess.svg
pgn = open('puzzle.pgn')
game = chess.pgn.read_game(pgn)
board = game.board()
def svg(i):
lm = board.peek()
a = [(lm.from_square, lm.to_square)]
s = chess.svg.board(board, arrows = a, orientation = chess.BLACK, size = 800)
f1 = "g_%04u.svg" % i
f2 = "g_%04u.png" % i
with open(f1, 'w') as f:
f.write(s)
os.system("convert %s %s" % (f1, f2))
os.system("rm %s" % f1)
i = 0
#try to find it!
while i < 10:
s = chess.svg.board(board, orientation = chess.BLACK, size = 800)
f1 = "g_%04u.svg" % i
f2 = "g_%04u.png" % i
with open(f1, 'w') as f:
f.write(s)
os.system("convert %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(3):
svg(i)
i += 1
# create animation with
# magick -delay 150 g_*.png -loop 0 g.gif
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.
http://creativecommons.org/publicdomain/zero/1.0/deed.enCC0Creative Commons Zero, Public Domain Dedicationfalsefalse