-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_chessboards.py
executable file
·107 lines (94 loc) · 4 KB
/
generate_chessboards.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
import os
from urllib import request
from io import BytesIO
import numpy as np
import PIL.Image
from constants import CHESSBOARDS_DIR, FEN_CHARS
# http://www.fen-to-image.com/image/32/rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
# http://jinchess.com/chessboard/?p=rnbqkbnrpppppppp--------------------------------PPPPPPPPRNBQKBNR
# https://chessdiagram.online/stilldiagram.php?d=_rnbqkbnrpppppppp________________________________PPPPPPPPRNBQKBNR
# https://chessdiagram.online/stagram.php?d=_rnbqkbnrpppppppp________________________________PPPPPPPPRNBQKBNR
# https://backscattering.de/web-boardimage/board.png?fen=rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
def generate_random_chessboards(n, img_url_template, fen_chars=FEN_CHARS) -> None:
""" Generates n random FEN diagrams from chess diagram template urls and
saves chessboard images to CHESSBOARDS_DIR
Output filenames show the pieces at squares from the top-left (a8) to the
bottom right (h1) of the board, with rows delimited by '-'. For example:
1bRqBQKq-11RBqkRP-BP1nq1b1-Q1PnkKPq-RPPkKNnr-RKp1pqPB-RRQRPPNQ-k1Ppn1qR.png
"""
if not os.path.exists(CHESSBOARDS_DIR):
os.makedirs(CHESSBOARDS_DIR)
output_dir = os.path.join(CHESSBOARDS_DIR, "generated")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for i in range(n):
fen_chars = list(fen_chars)
fen_arr = np.random.choice(fen_chars, 64)
if "fen-to-image.com" in img_url_template or "backscattering.de" in img_url_template:
fen_param = "/".join(map("".join, np.split(fen_arr, 8)))
else:
fen_param = "".join(fen_arr)
img_url = img_url_template.format(fen_param)
print(img_url)
img = PIL.Image.open(BytesIO(request.urlopen(img_url).read()))
if "chessdiagram.online" in img_url_template:
# need to flip FEN file order since the rows are 1-8 vs 8-1 of normal FEN.
fen_arr = np.hstack(np.split(fen_arr, 8)[::-1])
# Replace - or _ with 1 to be consistent with actual FEN notation
fen_arr[fen_arr == fen_chars[0]] = "1"
# Add '-' between sets of 8 to be consistent with saved file format
img_filename_prefix = "-".join(map("".join, np.split(fen_arr, 8)))
file_path = os.path.join(output_dir, img_filename_prefix + ".png")
print(file_path)
img.save(file_path)
def jinchess_img_url_template():
url_template = "http://jinchess.com/chessboard/?p={}"
jinchess_board_themes = [
None,
"cold-marble",
"gray-tiles",
"green-marble",
"pale-wood",
"red-marble",
"slate",
"winter",
"wooden-dark",
]
jinchess_piece_themes = [
None,
"merida-flat",
"smart-flat",
"usual-flat",
"alpha-flat",
]
theme = np.random.choice(jinchess_board_themes, 1)[0]
if theme is not None:
url_template += "&bp={}".format(theme)
pieces = np.random.choice(jinchess_piece_themes, 1)[0]
if pieces is not None:
url_template += "&ps={}".format(pieces)
if np.random.choice(2, 1)[0] == 1:
url_template += "&gs"
return url_template
if __name__ == '__main__':
# for i in range(100):
# generate_random_chessboards(1, jinchess_img_url_template(), "-KQRBNPkqrbnp")
generate_random_chessboards(
5, "http://www.fen-to-image.com/image/32/{}"
)
generate_random_chessboards(
5, "https://chessdiagram.online/stilldiagram.php?d=_{}&q=", "_KQRBNPkqrbnp"
)
generate_random_chessboards(
5, "https://chessdiagram.online/stagram.php?d=_{}&s=0&q=", "_KQRBNPkqrbnp"
)
generate_random_chessboards(
5, "https://chessdiagram.online/stagram.php?d=_{}&s=1&q=", "_KQRBNPkqrbnp"
)
generate_random_chessboards(
5, "https://chessdiagram.online/stagram.php?d=_{}&s=2&q=", "_KQRBNPkqrbnp"
)
# generate_random_chessboards(
# 1, "https://backscattering.de/web-boardimage/board.png?fen={}&size=256"
# )