is there a program to translate epd file to diagrams?

Discussion of anything and everything relating to chess playing software and machines.

Moderators: hgm, Harvey Williamson, bob

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Uri Blass
Posts: 8334
Joined: Wed Mar 08, 2006 11:37 pm
Location: Tel-Aviv Israel

is there a program to translate epd file to diagrams?

Post by Uri Blass » Sat Jan 12, 2019 2:58 pm

I generate epd file when I add every day positions to the file and I would like later to have a word file with diagrams of all the positions in the epd file.
I wonder if there is a free program that can translate the epd file to a file of diagrams that you can open by word.

I would like to have also a tool that translate only random part of the epd file that I decide about(for example if the epd file has 500 positions I would like an option to generate a word file of 50 random diagrams out of the 500).

The reason is that I would like the computer to generate a test for people who learn chess and if they are supposed to be able to find the best move in 500 positions but I do not want to ask them about all of them then I can test them only about random 50 positions and get a good estimate how much they know.

User avatar
xr_a_y
Posts: 394
Joined: Sat Nov 25, 2017 1:28 pm
Location: France

Re: is there a program to translate epd file to diagrams?

Post by xr_a_y » Sat Jan 12, 2019 4:00 pm

I can convert to png at least, in python :

Code: Select all

import chess, sys, os, re
from chess import svg
from cairosvg import svg2png

fname = sys.argv[1]
print("Opening ",fname)
with open(fname) as f:
    content = f.readlines()

rand_items = random.sample(content, 50)

for fen in rand_items:
    print("writing fen ", fen)
    board = chess.Board(fen)
    nameoffile = fen.replace("/", "_").replace(" ","-")
    boardsvg = chess.svg.board(board = board)
    filetowriteto = open(nameoffile, "w")
    filetowriteto.write(boardsvg)
    filetowriteto.close()
    svg2png(open(nameoffile, 'rb').read(), write_to=open(nameoffile+".png", 'wb'))
    os.remove(nameoffile)
Then the png image can easily be assemble into a unique pdf file.

User avatar
Guenther
Posts: 2701
Joined: Wed Oct 01, 2008 4:33 am
Location: Regensburg, Germany
Full name: Guenther Simon
Contact:

Re: is there a program to translate epd file to diagrams?

Post by Guenther » Sat Jan 12, 2019 4:22 pm

Uri Blass wrote:
Sat Jan 12, 2019 2:58 pm
I generate epd file when I add every day positions to the file and I would like later to have a word file with diagrams of all the positions in the epd file.
I wonder if there is a free program that can translate the epd file to a file of diagrams that you can open by word.

I would like to have also a tool that translate only random part of the epd file that I decide about(for example if the epd file has 500 positions I would like an option to generate a word file of 50 random diagrams out of the 500).

The reason is that I would like the computer to generate a test for people who learn chess and if they are supposed to be able to find the best move in 500 positions but I do not want to ask them about all of them then I can test them only about random 50 positions and get a good estimate how much they know.
viewtopic.php?f=2&t=61806

Ferdy
Posts: 3713
Joined: Sun Aug 10, 2008 1:15 pm
Location: Philippines

Re: is there a program to translate epd file to diagrams?

Post by Ferdy » Sun Jan 13, 2019 3:37 am

Uri Blass wrote:
Sat Jan 12, 2019 2:58 pm
I generate epd file when I add every day positions to the file and I would like later to have a word file with diagrams of all the positions in the epd file.
I wonder if there is a free program that can translate the epd file to a file of diagrams that you can open by word.
This one will output board images to html file. It uses python-chess svg rendering. Board is flipped depending on the side to move.

Image

command line:
python b.py wacnew.epd magnus_test1.html random 10

usage:
python b.py wacnew.epd magnus_test1.html [random | seq] 5

You need python 3 and python-chess installed. Your epd also needs to have an id opcode because that will be shown as title after the image.

Source code on b.py

Code: Select all

"""
Create html file with chess board from epd file.   

"""


import sys
from random import shuffle
import chess
import chess.svg


def create_html(fn, images):
    """ fn = output html file, images = list of image filenames """
    total_pos = len(images)
    
    with open(fn, 'w') as h:
        h.write('<!DOCTYPE html>\n')
        h.write('<html>\n')
        h.write('<head>\n')
        h.write('</head>\n')
        h.write('<body>\n')    
        
        h.write('<center>\n')
        
        h.write('<p>File: %s,\n' % fn[:-5])
        h.write('NumPos: %d,\n' % total_pos)
        h.write('Score: ______</p>\n')

        for im in images:            
            im_file = im[0]
            im_side = 'White to move' if im[1] else 'Black to move'
            im_title = im[2]
            
            h.write('<object type="image/svg+xml" data=\"%s\"></object><br>\n' % im_file)
            h.write('%s, %s<br>\n' % (im_side, im_title))
            h.write('- - - - - - - - - - - - -<br>\n')
            
        h.write('</center>\n')        
        h.write('</body>\n')
        h.write('</html>\n')
            

def create_images(outfn, epd_list, im_size, add_coor, max_im_num, is_random):
    """ Returns a list of image info [img_file, pos_side, epd_id] """
    if is_random:
        shuffle(epd_list)

    cnt = 0
    images = []
    
    for epd in epd_list:
        cnt += 1
        fn = '_'.join(outfn[:-5].split()) + '_pos_' + str(cnt) + '.svg'
        
        board = chess.Board()
        epd_info = board.set_epd(epd)

        epd_id = epd_info['id']
        side = board.turn
        flip = False if side else True  # Flip board if black to move
        
        images.append([fn, side, epd_id])
        img = chess.svg.board(board=board, size=im_size, flipped=flip, coordinates=add_coor)

        with open(fn, 'w') as h:
            h.write(img)
            
        if cnt >= max_im_num:
            break
        
    return images


def main(argv):    
    try:
        epdfn = argv[0]
        outfn = argv[1]
        
        is_random = True if argv[2] == 'random' else False
        max_image_num = int(argv[3])
    except:
        print('Usage: Use python 3 and latest version of python-chess')
        print('python b.py wacnew.epd magnus_test1.html [random | seq] 5')
        return        
    
    image_size = 250  # In pixels
    add_coor = True
    
    # Read epd file and save each epd to a list
    epd_list = []
    with open(epdfn, 'r') as h:
        for epd in h:
            epd_list.append(epd.strip())

    # Create svg files
    images = create_images(outfn, epd_list, image_size, add_coor, max_image_num, is_random)
        
    # Create html
    create_html(outfn, images)
    

if __name__ == "__main__":
    main(sys.argv[1:])

User avatar
Guenther
Posts: 2701
Joined: Wed Oct 01, 2008 4:33 am
Location: Regensburg, Germany
Full name: Guenther Simon
Contact:

Re: is there a program to translate epd file to diagrams?

Post by Guenther » Sun Jan 13, 2019 9:41 am

Ferdy wrote:
Sun Jan 13, 2019 3:37 am
Uri Blass wrote:
Sat Jan 12, 2019 2:58 pm
I generate epd file when I add every day positions to the file and I would like later to have a word file with diagrams of all the positions in the epd file.
I wonder if there is a free program that can translate the epd file to a file of diagrams that you can open by word.
This one will output board images to html file. It uses python-chess svg rendering. Board is flipped depending on the side to move.
Thanks a lot Ferdinand. This works like a charm! I have added it to my chesstools.
Would it be much work to add column output as a variable, to fill the width of the html page?
(table structure with x columns)

Ignacio
Posts: 142
Joined: Wed Mar 08, 2006 7:15 pm

Re: is there a program to translate epd file to diagrams?

Post by Ignacio » Sun Jan 13, 2019 11:56 am


Ferdy
Posts: 3713
Joined: Sun Aug 10, 2008 1:15 pm
Location: Philippines

Re: is there a program to translate epd file to diagrams?

Post by Ferdy » Sun Jan 13, 2019 1:25 pm

Guenther wrote:
Sun Jan 13, 2019 9:41 am
Ferdy wrote:
Sun Jan 13, 2019 3:37 am
Uri Blass wrote:
Sat Jan 12, 2019 2:58 pm
I generate epd file when I add every day positions to the file and I would like later to have a word file with diagrams of all the positions in the epd file.
I wonder if there is a free program that can translate the epd file to a file of diagrams that you can open by word.
This one will output board images to html file. It uses python-chess svg rendering. Board is flipped depending on the side to move.
Thanks a lot Ferdinand. This works like a charm! I have added it to my chesstools.
Would it be much work to add column output as a variable, to fill the width of the html page?
(table structure with x columns)
Try this.

command line:
python b.py arasan20.epd arasan_test.html 4 seq 50 250 coor

Image

usage:
python b.py <epd filename> <html filename> <html num_column> <[random | seq]> <num_pos> <image_size> <[coor | nocoor]>

b.py

Code: Select all

"""
Create html file with chess board from epd file.   

"""


import sys
from random import shuffle
import chess
import chess.svg


def create_html(fn, images, col_num):
    """ fn = output html file, images = list of image filenames """
    total_pos = len(images)
    
    with open(fn, 'w') as h:
        h.write('<!DOCTYPE html>\n')
        h.write('<html>\n')
        h.write('<head>\n')
        
        h.write('<style>\n')
        h.write('.board-column {\n')
        h.write('  -webkit-column-count: %d;\n' % col_num)
        h.write('  -moz-column-count: %d;\n' % col_num)
        h.write('  column-count: %d;\n' % col_num)
        h.write('  -webkit-column-width: 100px;\n')
        h.write('  -moz-column-width: 100px;\n')
        h.write('  column-width: 100px;\n')
        h.write('}\n')
        h.write('</style>\n')
        
        h.write('</head>\n')
        h.write('<body>\n')    
        
        h.write('<center>\n')
        
        h.write('<p>File: %s,\n' % fn[:-5])
        h.write('NumPos: %d,\n' % total_pos)
        h.write('Score: ______</p>\n')
        h.write('</center>\n')
        
        h.write('<div class=board-column>\n')
        
        h.write('<center>\n')

        for im in images:            
            im_file = im[0]
            im_side = 'White to move' if im[1] else 'Black to move'
            im_title = im[2]
            
            h.write('<div>\n')            
            h.write('<object type="image/svg+xml" data=\"%s\"></object><br>\n' % im_file)
            h.write('%s, %s<br>\n' % (im_side, im_title))
            h.write('- - - - - - - - - - - - -<br><br>\n')
            h.write('</div>\n')
            
        h.write('</center>\n')

        h.write('</div>\n')

        h.write('</body>\n')
        h.write('</html>\n')
            

def create_images(outfn, epd_list, im_size, add_coor, max_im_num, is_random):
    """ Returns a list of image info [img_file, pos_side, epd_id] """
    if is_random:
        shuffle(epd_list)

    cnt = 0
    images = []
    
    for epd in epd_list:
        cnt += 1
        fn = '_'.join(outfn[:-5].split()) + '_pos_' + str(cnt) + '.svg'
        
        board = chess.Board()
        epd_info = board.set_epd(epd)

        epd_id = epd_info['id']
        side = board.turn
        flip = False if side else True  # Flip board if black to move
        
        images.append([fn, side, epd_id])
        img = chess.svg.board(board=board, size=im_size, flipped=flip, coordinates=add_coor)

        with open(fn, 'w') as h:
            h.write(img)
            
        if cnt >= max_im_num:
            break
        
    return images


def main(argv):    
    try:
        epdfn = argv[0]
        
        outfn = argv[1]  # html filename
        html_col_num = int(argv[2])
        
        is_random = True if argv[3] == 'random' else False
        max_image_num = int(argv[4])
        image_size = int(argv[5])
        add_coor = True if argv[6] == 'coor' else False
        
    except:
        print('Usage: Use python 3 and latest version of python-chess')
        print('python b.py <epd filename> <html filename> <html num_column> <[random | seq]> <num_pos> <image_size> <[coor | nocoor]>')
        return
    
    # Read epd file and save each epd to a list
    epd_list = []
    with open(epdfn, 'r') as h:
        for epd in h:
            epd_list.append(epd.strip())

    # Create svg files
    images = create_images(outfn, epd_list, image_size, add_coor, max_image_num, is_random)
        
    # Create html
    create_html(outfn, images, html_col_num)
    

if __name__ == "__main__":
    main(sys.argv[1:])

User avatar
Guenther
Posts: 2701
Joined: Wed Oct 01, 2008 4:33 am
Location: Regensburg, Germany
Full name: Guenther Simon
Contact:

Re: is there a program to translate epd file to diagrams?

Post by Guenther » Sun Jan 13, 2019 4:50 pm

Ferdy wrote:
Sun Jan 13, 2019 1:25 pm

Try this.
Thanks a lot!

Ferdy
Posts: 3713
Joined: Sun Aug 10, 2008 1:15 pm
Location: Philippines

Re: is there a program to translate epd file to diagrams?

Post by Ferdy » Mon Jan 14, 2019 12:51 am

Guenther wrote:
Sun Jan 13, 2019 4:50 pm
Ferdy wrote:
Sun Jan 13, 2019 1:25 pm

Try this.
Thanks a lot!
Added a way to handle an epd without id.

Code: Select all

       # Handle epd without id
        try:
            epd_id = epd_info['id']
        except KeyError:
            print('Warning! this epd has no id')
            epd_id = 'id ' + str(cnt)
Full code on b.py

Code: Select all

"""
Create html file with chess board from epd file.   

"""


import sys
from random import shuffle
import chess
import chess.svg


def create_html(fn, images, col_num):
    """ fn = output html file, images = list of image filenames """
    total_pos = len(images)
    
    with open(fn, 'w') as h:
        h.write('<!DOCTYPE html>\n')
        h.write('<html>\n')
        h.write('<head>\n')
        
        h.write('<style>\n')
        h.write('.board-column {\n')
        h.write('  -webkit-column-count: %d;\n' % col_num)
        h.write('  -moz-column-count: %d;\n' % col_num)
        h.write('  column-count: %d;\n' % col_num)
        h.write('  -webkit-column-width: 100px;\n')
        h.write('  -moz-column-width: 100px;\n')
        h.write('  column-width: 100px;\n')
        h.write('}\n')
        h.write('</style>\n')
        
        h.write('</head>\n')
        h.write('<body>\n')    
        
        h.write('<center>\n')
        
        h.write('<p>File: %s,\n' % fn[:-5])
        h.write('NumPos: %d,\n' % total_pos)
        h.write('Score: ______</p>\n')
        h.write('</center>\n')
        
        h.write('<div class=board-column>\n')
        
        h.write('<center>\n')

        for im in images:            
            im_file = im[0]
            im_side = 'White to move' if im[1] else 'Black to move'
            im_title = im[2]
            
            h.write('<div>\n')            
            h.write('<object type="image/svg+xml" data=\"%s\"></object><br>\n' % im_file)
            h.write('%s, %s<br>\n' % (im_side, im_title))
            h.write('- - - - - - - - - - - - -<br><br>\n')
            h.write('</div>\n')
            
        h.write('</center>\n')

        h.write('</div>\n')

        h.write('</body>\n')
        h.write('</html>\n')
            

def create_images(outfn, epd_list, im_size, add_coor, max_im_num, is_random):
    """ Returns a list of image info [img_file, pos_side, epd_id] """
    if is_random:
        shuffle(epd_list)

    cnt = 0
    images = []
    
    for epd in epd_list:
        cnt += 1
        fn = '_'.join(outfn[:-5].split()) + '_pos_' + str(cnt) + '.svg'
        
        board = chess.Board()
        epd_info = board.set_epd(epd)
        
        # Handle epd without id
        try:
            epd_id = epd_info['id']
        except KeyError:
            print('Warning! this epd has no id')
            epd_id = 'id ' + str(cnt) 
        side = board.turn
        flip = False if side else True  # Flip board if black to move
        
        images.append([fn, side, epd_id])
        img = chess.svg.board(board=board, size=im_size, flipped=flip, coordinates=add_coor)

        with open(fn, 'w') as h:
            h.write(img)
            
        if cnt >= max_im_num:
            break
        
    return images


def main(argv):    
    try:
        epdfn = argv[0]
        
        outfn = argv[1]  # html filename
        html_col_num = int(argv[2])
        
        is_random = True if argv[3] == 'random' else False
        max_image_num = int(argv[4])
        image_size = int(argv[5])
        add_coor = True if argv[6] == 'coor' else False
        
    except:
        print('Usage: Use python 3 and latest version of python-chess')
        print('python b.py <epd filename> <html filename> <html num_column> <[random | seq]> <num_pos> <image_size> <[coor | nocoor]>')
        return
    
    # Read epd file and save each epd to a list
    epd_list = []
    with open(epdfn, 'r') as h:
        for epd in h:
            epd_list.append(epd.strip())

    # Create svg files
    images = create_images(outfn, epd_list, image_size, add_coor, max_image_num, is_random)
        
    # Create html
    create_html(outfn, images, html_col_num)
    

if __name__ == "__main__":
    main(sys.argv[1:])

Uri Blass
Posts: 8334
Joined: Wed Mar 08, 2006 11:37 pm
Location: Tel-Aviv Israel

Re: is there a program to translate epd file to diagrams?

Post by Uri Blass » Mon Jan 14, 2019 7:36 am

Thanks for the replies.

Not sure what I am supposed to do exactly.

Do I need to copy the programs in python first to my computer?
searching in my computer I see that I have python2.7.3 in my computer but I never used it and never wrote code in it.

step by step instruction what to do can be productive because I do not want to guess what to do.

Edit:I wonder if it works on the following epd file that I created based on positions from one game in the world blitz women championship
when in all the positions there is a move that is significantly better than other moves.

r2qkb1r/pp1npppp/2p2n2/3P1b2/8/1P3NP1/PB1PPP1P/RN1QKB1R b KQkq - 0 6 bm cxd5(not Nxd5) easy positional problem
r2qk2r/1p1n1pp1/p2bpn1p/3p1b2/8/1PNP1NP1/PB2PPBP/R2Q1RK1 w kq - 0 11 bm e4
r2qk2r/1p1n1pp1/p2bpn1p/3p4/4P1b1/1PNP1NP1/PB3PBP/R2Q1RK1 w kq - 1 12 bm exd5
r2q1k1r/1p1n1pp1/p2b1n1p/3p4/6b1/1PNP1NPP/PB3PB1/R2QR1K1 b - - 0 14 bm Bh5
r2q2kr/1p1n1pp1/p2b1n1p/3p3b/6P1/1P1P1N1P/PB2NPB1/R2QR1K1 b - - 0 16 bm Bg6 easy
r2q3r/1p1n1ppk/p4nNp/3p4/1b1N2P1/1P1P3P/PB3PB1/R2QR1K1 b - - 0 19 bm fxg6 easy positional problem
r2q3r/1p1n2pk/p3Rnpp/3p4/1b1N2P1/1P1P3P/PB3PB1/R2Q2K1 b - - 1 20 bm Re8 positional problem
2rqr3/1p1n2pk/p3Rnpp/3p4/3N2P1/PPbP1Q1P/1B3PB1/R5K1 w - - 1 23 bm Bxc3
2rqr3/1p1n2pk/p3Rnpp/3p4/3N2P1/PPBP1Q1P/5PB1/R5K1 b - - 0 23 bm Rxc3(easy tactical)
3qr3/1p1n2pk/p3Rnpp/3p4/3N2P1/PPrP1Q1P/5PB1/R5K1 w - - 0 24 bm Rae1
3q4/1p1n2pk/p3rnpp/3p4/3N2P1/PPrP1Q1P/5PB1/4R1K1 w - - 0 25 bm Nxe6(Rxe6 lose a pawn)
8/1p1n2pk/pq2Nnpp/3p4/6P1/PPrP1Q1P/5PB1/4R1K1 w - - 1 26 bm g5

Post Reply