diff --git a/.gitignore b/.gitignore index 7049bf9..49abbaf 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,7 @@ venv/ .idea/ .DS_Store __pycache__/ +.pstats +*.egg-info/ +dist/ settings.py diff --git a/README.md b/README.md index d761802..4a89454 100644 --- a/README.md +++ b/README.md @@ -61,3 +61,20 @@ game.move_from_coordinates(game.player2, 6, 2, 4, 2) game.move_from_coordinates(game.player2, 2, 5, 6, 5) ``` There are colors in the command line not showing here in the GIF, though... + +## How to change interface colors + +With line command you can create new illustrations by specifying the RGB colors you want. You can specify the colors of the cells and of the pieces (outer and inner colors). +Use the following command with the colors you want: +```bash +python interface/colorize.py -ccb 0 191 255 -ccw 207 185 151 -ibp 109 7 26 -obp 0 0 0 -iwp 200 200 200 -owp 255 255 255 +``` +Arguments are: +- -ccb/--color_cell_black: RGB, default 0, 191, 255 +- -ccw/--color_cell_white: RGB, default 207, 185, 151 +- -ibp/--inner_black_piece: RGB, default 109, 7, 26 +- -obp/--outer_black_piece: RGB, default 0, 0, 0 +- -iwp/--inner_white_piece: RGB, default 200, 200, 200 +- -owp/--outer_white_piece: RGB, default 255, 255, 255 + +The command creates (or replaces) the illustrations that are in the temp_images/ folder. If the folder exists, the interface uses theses illustrations. Destroy it to come back to standard illustrations. diff --git a/dev-illustrations/_bishop_grey.png b/dev-illustrations/_bishop_grey.png new file mode 100644 index 0000000..8bbfd43 Binary files /dev/null and b/dev-illustrations/_bishop_grey.png differ diff --git a/dev-illustrations/_bishop_orange.png b/dev-illustrations/_bishop_orange.png new file mode 100644 index 0000000..9a4fffb Binary files /dev/null and b/dev-illustrations/_bishop_orange.png differ diff --git a/dev-illustrations/king_grey.png b/dev-illustrations/king_grey.png new file mode 100644 index 0000000..e553fcc Binary files /dev/null and b/dev-illustrations/king_grey.png differ diff --git a/dev-illustrations/king_orange.png b/dev-illustrations/king_orange.png new file mode 100644 index 0000000..adf27dd Binary files /dev/null and b/dev-illustrations/king_orange.png differ diff --git a/dev-illustrations/knight_grey.png b/dev-illustrations/knight_grey.png new file mode 100644 index 0000000..d9cdec3 Binary files /dev/null and b/dev-illustrations/knight_grey.png differ diff --git a/dev-illustrations/knight_orange.png b/dev-illustrations/knight_orange.png new file mode 100644 index 0000000..acb078a Binary files /dev/null and b/dev-illustrations/knight_orange.png differ diff --git a/dev-illustrations/pawn_grey.png b/dev-illustrations/pawn_grey.png new file mode 100644 index 0000000..8884500 Binary files /dev/null and b/dev-illustrations/pawn_grey.png differ diff --git a/dev-illustrations/pawn_orange.png b/dev-illustrations/pawn_orange.png new file mode 100644 index 0000000..09bc093 Binary files /dev/null and b/dev-illustrations/pawn_orange.png differ diff --git a/dev-illustrations/queen_grey.png b/dev-illustrations/queen_grey.png new file mode 100644 index 0000000..c31709c Binary files /dev/null and b/dev-illustrations/queen_grey.png differ diff --git a/dev-illustrations/queen_orange.png b/dev-illustrations/queen_orange.png new file mode 100644 index 0000000..18ff99f Binary files /dev/null and b/dev-illustrations/queen_orange.png differ diff --git a/dev-illustrations/rook_grey.png b/dev-illustrations/rook_grey.png new file mode 100644 index 0000000..a06c741 Binary files /dev/null and b/dev-illustrations/rook_grey.png differ diff --git a/dev-illustrations/rook_orange.png b/dev-illustrations/rook_orange.png new file mode 100644 index 0000000..120fb6b Binary files /dev/null and b/dev-illustrations/rook_orange.png differ diff --git a/gs_illustrations/bishop_gray.png b/gs_illustrations/bishop_gray.png new file mode 100644 index 0000000..17d6e17 Binary files /dev/null and b/gs_illustrations/bishop_gray.png differ diff --git a/gs_illustrations/king_gray.png b/gs_illustrations/king_gray.png new file mode 100644 index 0000000..fabdf35 Binary files /dev/null and b/gs_illustrations/king_gray.png differ diff --git a/gs_illustrations/knight_gray.png b/gs_illustrations/knight_gray.png new file mode 100644 index 0000000..92bc809 Binary files /dev/null and b/gs_illustrations/knight_gray.png differ diff --git a/gs_illustrations/pawn_gray.png b/gs_illustrations/pawn_gray.png new file mode 100644 index 0000000..ea15c1d Binary files /dev/null and b/gs_illustrations/pawn_gray.png differ diff --git a/gs_illustrations/queen_gray.png b/gs_illustrations/queen_gray.png new file mode 100644 index 0000000..a61a552 Binary files /dev/null and b/gs_illustrations/queen_gray.png differ diff --git a/gs_illustrations/rook_gray.png b/gs_illustrations/rook_gray.png new file mode 100644 index 0000000..7f58eea Binary files /dev/null and b/gs_illustrations/rook_gray.png differ diff --git a/pyalapin/interface/color_edition.py b/pyalapin/interface/color_edition.py new file mode 100644 index 0000000..b6dd1f8 --- /dev/null +++ b/pyalapin/interface/color_edition.py @@ -0,0 +1,205 @@ +import numpy as np +from PIL import Image + +from settings import settings + + +class ColorEditor(object): + def __init__( + self, + white_cell, + down_white_cell, + black_cell, + down_black_cell, + white_piece_fill, + white_piece_border, + black_piece_fill, + black_piece_border, + ): + self.white_cell = white_cell + self.down_white_cell = down_white_cell + self.black_cell = black_cell + self.down_black_cell = down_black_cell + self.white_piece_fill = white_piece_fill + self.white_piece_border = white_piece_border + self.black_piece_fill = black_piece_fill + self.black_piece_border = black_piece_border + + def get_edited_black_cell(self): + return np.array([[self.black_cell] * 64] * 64), np.array( + [[self.down_black_cell] * 64] * 64 + ) + + def get_edited_white_cell(self): + return np.array([[self.white_cell] * 64] * 64), np.array( + [[self.down_white_cell] * 64] * 64 + ) + + def get_edited_black_material(self, material_type, cell_color): + if cell_color == "black": + cell_color = self.black_cell + else: + cell_color = self.white_cell + + image = Image.open(settings["grayscale_images"][material_type]) + r_map = { + 0: cell_color[0], + 128: self.black_piece_fill[0], + 255: self.black_piece_border[0], + } + g_map = { + 0: cell_color[1], + 128: self.black_piece_fill[1], + 255: self.black_piece_border[1], + } + b_map = { + 0: cell_color[2], + 128: self.black_piece_fill[2], + 255: self.black_piece_border[2], + } + + edited_img = np.array(image) + edited_img = np.dstack( + [ + np.vectorize(r_map.get)(edited_img), + np.vectorize(g_map.get)(edited_img), + np.vectorize(b_map.get)(edited_img), + ] + ) + + if cell_color == "black": + cell_color = self.down_black_cell + else: + cell_color = self.down_white_cell + + r_map = { + 0: cell_color[0], + 128: self.black_piece_fill[0], + 255: self.black_piece_border[0], + } + g_map = { + 0: cell_color[1], + 128: self.black_piece_fill[1], + 255: self.black_piece_border[1], + } + b_map = { + 0: cell_color[2], + 128: self.black_piece_fill[2], + 255: self.black_piece_border[2], + } + + down_edited_img = np.array(image) + down_edited_img = np.dstack( + [ + np.vectorize(r_map.get)(edited_img), + np.vectorize(g_map.get)(edited_img), + np.vectorize(b_map.get)(edited_img), + ] + ) + + return edited_img, down_edited_img + + def get_edited_white_material(self, material_type, cell_color): + if cell_color == "black": + cell_color = self.black_cell + else: + cell_color = self.white_cell + + image = Image.open(settings["grayscale_images"][material_type]) + r_map = { + 0: cell_color[0], + 128: self.white_piece_fill[0], + 255: self.white_piece_border[0], + } + g_map = { + 0: cell_color[1], + 128: self.white_piece_fill[1], + 255: self.white_piece_border[1], + } + b_map = { + 0: cell_color[2], + 128: self.white_piece_fill[2], + 255: self.white_piece_border[2], + } + + edited_img = np.array(image) + edited_img = np.dstack( + [ + np.vectorize(r_map.get)(edited_img), + np.vectorize(g_map.get)(edited_img), + np.vectorize(b_map.get)(edited_img), + ] + ) + + if cell_color == "black": + cell_color = self.down_black_cell + else: + cell_color = self.down_white_cell + + r_map = { + 0: cell_color[0], + 128: self.white_piece_fill[0], + 255: self.white_piece_border[0], + } + g_map = { + 0: cell_color[1], + 128: self.white_piece_fill[1], + 255: self.white_piece_border[1], + } + b_map = { + 0: cell_color[2], + 128: self.white_piece_fill[2], + 255: self.white_piece_border[2], + } + + down_edited_img = np.array(image) + down_edited_img = np.dstack( + [ + np.vectorize(r_map.get)(edited_img), + np.vectorize(g_map.get)(edited_img), + np.vectorize(b_map.get)(edited_img), + ] + ) + + return edited_img + + def generate_all_images(self, path="temp_illustrations"): + b, down_b = self.get_edited_black_cell() + Image.from_array(b).save(os.path.join(path, "b.png")) + Image.from_array(down_b).save(os.path.join(path, "down_b.png")) + w, down_w = self.get_edited_black_cell() + Image.from_array(w).save(os.path.join(path, "w.png")) + Image.from_array(down_w).save(os.path.join(path, "down_w.png")) + for piece in ["pawn", "bishop", "knight", "rook", "king", "queen"]: + wb_p, down_wb_p = self.get_edited_black_material( + material_type=piece, cell_color="white" + ) + bb_p, down_bb_p = self.get_edited_black_material( + material_type=piece, cell_color="black" + ) + ww_p, down_ww_p = self.get_edited_white_material( + material_type=piece, cell_color="white" + ) + bw_p, down_bw_p = self.get_edited_white_material( + material_type=piece, cell_color="black" + ) + prefix = "N" if piece == "knight" else piece[0].upper() + Image.from_array(wb_p).save(os.path.join(path, f"wb_{prefix}.png")) + Image.from_array(down_wb_p).save( + os.path.join(path, f"down_wb_{prefix}.png") + ) + + Image.from_array(bb_p).save(os.path.join(path, f"bb_{prefix}.png")) + Image.from_array(down_bb_p).save( + os.path.join(path, f"down_bb_{prefix}.png") + ) + + Image.from_array(ww_p).save(os.path.join(path, f"ww_{prefix}.png")) + Image.from_array(down_ww_p).save( + os.path.join(path, f"down_ww_{prefix}.png") + ) + + Image.from_array(bw_p).save(os.path.join(path, f"bw_{prefix}.png")) + Image.from_array(down_bw_p).save( + os.path.join(path, f"down_bw_{prefix}.png") + ) diff --git a/pyalapin/interface/colorize.py b/pyalapin/interface/colorize.py new file mode 100644 index 0000000..90f0e52 --- /dev/null +++ b/pyalapin/interface/colorize.py @@ -0,0 +1,147 @@ +import argparse +import os + +from PIL import Image +import numpy as np + + +def color_one_img(pil_img, background_color, border_color, inner_color): + img = np.array(pil_img) + x_min = np.min(np.where(img == 255)[0]) + x_max = np.max(np.where(img == 255)[0]) + img = img[x_min:x_max] + + r_dict = {0: background_color[0], 128: inner_color[0], 255: border_color[0]} + g_dict = {0: background_color[1], 128: inner_color[1], 255: border_color[1]} + b_dict = {0: background_color[2], 128: inner_color[2], 255: border_color[2]} + + img = [ + np.vectorize(r_dict.get)(img), + np.vectorize(g_dict.get)(img), + np.vectorize(b_dict.get)(img), + ] + img = np.dstack(img) + + img = Image.fromarray(img.astype("uint8")) + + img = img.resize((int(img.size[0] / img.size[1] * 450), 450)) + + x_coordinate = int((500 - img.size[0]) / 2) + y_coordinate = 25 + + background = np.dstack( + [np.ones((500, 500)) * background_color[i] for i in range(3)] + ).astype("uint8") + background = Image.fromarray(background) + background.paste(img, (x_coordinate, y_coordinate)) + return background + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("-ccb", "--color_cell_black", nargs="+") + parser.add_argument("-ccw", "--color_cell_white", nargs="+") + + parser.add_argument("-ibp", "--inner_black_piece", nargs="+") + parser.add_argument("-obp", "--outer_black_piece", nargs="+") + + parser.add_argument("-iwp", "--inner_white_piece", nargs="+") + parser.add_argument("-owp", "--outer_white_piece", nargs="+") + + args = parser.parse_args() + + if args.color_cell_black is None: + color_cell_b = [0, 191, 255] + else: + color_cell_b = [int(i) for i in args.color_cell_black] + + if args.color_cell_white is None: + color_cell_w = [207, 185, 151] + else: + color_cell_w = [int(i) for i in args.color_cell_white] + + if args.inner_black_piece is None: + inner_color_b = [109, 7, 26] + else: + inner_color_b = [int(i) for i in args.inner_black_piece] + if args.outer_black_piece is None: + border_color_b = [0, 0, 0] + else: + border_color_b = [int(i) for i in args.outer_black_piece] + + if args.inner_white_piece is None: + inner_color_w = [200, 200, 200] + else: + inner_color_w = [int(i) for i in args.inner_white_piece] + if args.outer_white_piece is None: + border_color_w = [255, 255, 255] + else: + border_color_w = [int(i) for i in args.outer_white_piece] + + down_color_cell_b = np.clip(np.array(color_cell_b) - 70, 0, 255) + down_color_cell_w = np.clip(np.array(color_cell_w) - 70, 0, 255) + + for img_name in os.listdir("../gs_illustrations"): + img = Image.open(os.path.join("../gs_illustrations", img_name)) + img_bb = color_one_img(img, color_cell_b, border_color_b, inner_color_b) + img_ww = color_one_img(img, color_cell_w, border_color_w, inner_color_w) + img_wb = color_one_img(img, color_cell_w, border_color_b, inner_color_b) + img_bw = color_one_img(img, color_cell_b, border_color_w, inner_color_w) + + down_img_bb = color_one_img( + img, down_color_cell_b, border_color_b, inner_color_b + ) + down_img_ww = color_one_img( + img, down_color_cell_w, border_color_w, inner_color_w + ) + down_img_wb = color_one_img( + img, down_color_cell_w, border_color_b, inner_color_b + ) + down_img_bw = color_one_img( + img, down_color_cell_b, border_color_w, inner_color_w + ) + + name = img_name.split("_")[0] + name = { + "pawn": "P", + "bishop": "B", + "knight": "N", + "rook": "R", + "queen": "Q", + "king": "K", + }[name] + img_bb.save(os.path.join("../temp_images", f"bb_{name}.png")) + img_bw.save(os.path.join("../temp_images", f"bw_{name}.png")) + img_wb.save(os.path.join("../temp_images", f"wb_{name}.png")) + img_ww.save(os.path.join("../temp_images", f"ww_{name}.png")) + + down_img_bb.save(os.path.join("../temp_images", f"down_bb_{name}.png")) + down_img_bw.save(os.path.join("../temp_images", f"down_bw_{name}.png")) + down_img_wb.save(os.path.join("../temp_images", f"down_wb_{name}.png")) + down_img_ww.save(os.path.join("../temp_images", f"down_ww_{name}.png")) + + bg_w = Image.fromarray( + np.dstack([np.ones((500, 500)) * color_cell_w[i] for i in range(3)]).astype( + "uint8" + ) + ) + bg_w.save(os.path.join("../temp_images", "w.png")) + bg_b = Image.fromarray( + np.dstack([np.ones((500, 500)) * color_cell_b[i] for i in range(3)]).astype( + "uint8" + ) + ) + bg_b.save(os.path.join("../temp_images", "b.png")) + + down_bg_w = Image.fromarray( + np.dstack( + [np.ones((500, 500)) * down_color_cell_w[i] for i in range(3)] + ).astype("uint8") + ) + down_bg_w.save(os.path.join("../temp_images", "down_w.png")) + down_bg_b = Image.fromarray( + np.dstack( + [np.ones((500, 500)) * down_color_cell_b[i] for i in range(3)] + ).astype("uint8") + ) + down_bg_b.save(os.path.join("../temp_images", "down_b.png")) diff --git a/pyalapin/interface/interface.py b/pyalapin/interface/interface.py index a60d0eb..fa4997f 100644 --- a/pyalapin/interface/interface.py +++ b/pyalapin/interface/interface.py @@ -14,27 +14,6 @@ from pyalapin.engine.engine import ChessGame -class LoginScreen(GridLayout): - def __init__(self, **kwargs): - """ - Base class for a Login Screen, not used yet - """ - super(LoginScreen, self).__init__(**kwargs) - self.cols = 8 - self.add_widget(Label(text="")) - self.username = TextInput(multiline=False) - self.add_widget(self.username) - self.add_widget(Label(text="password")) - self.password = TextInput(password=True, multiline=False) - self.add_widget(self.password) - - self.add_widget(Label(text="password2")) - self.password2 = TextInput(password=True, multiline=False) - self.add_widget(self.password2) - - self.add_widget(Rectangle(pos=(10, 10), size=(500, 500))) - - class DisplayableCell(Button): """Base class to represent a Cell as Button""" @@ -89,7 +68,18 @@ def __init__(self, game, **kwargs): """ super(BoardInterface, self).__init__(**kwargs) - self.path_to_illustrations = "illustrations" + if os.path.isdir("temp_images"): + imgs = [] + for z in os.listdir("temp_images"): + if "png" in z: + imgs.append(z) + if len(imgs) == len(os.listdir("illustrations")): + self.path_to_illustrations = "temp_images" + else: + self.path_to_illustrations = "illustrations" + else: + self.path_to_illustrations = "illustrations" + self.game = game if game.ai: @@ -388,7 +378,7 @@ def click_cell(self, event): class ChessApp(App): """ - Main app to use to play game, by calling MyApp().buil() and then player. + Main app to use to play game, by calling ChessApp().build() and then players. """ def __init__(self, play_with_ai=False, w_player=None, b_player=None, **kwargs): diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..1b915ac --- /dev/null +++ b/settings.py @@ -0,0 +1,11 @@ +settings = { + "stockfish_path": "/Users/vincent.auriau/Python/stockfish/bin/stockfish", + "grayscale_images": { + "pawn": "gs_illustrations/pawn_gray.png", + "bishop": "gs_illustrations/bishop_gray.png", + "knight": "gs_illustrations/knight_gray.png", + "rook": "gs_illustrations/rook_gray.png", + "king": "gs_illustrations/king_gray.png", + "queen": "gs_illustrations/queen_gray.png", + }, +} diff --git a/utils/create_base.py b/utils/create_base.py new file mode 100644 index 0000000..4bb1de1 --- /dev/null +++ b/utils/create_base.py @@ -0,0 +1,161 @@ +import matplotlib.pyplot as plt +import numpy as np + +from PIL import Image + +black_pawn = np.array(Image.open("../dev-illustrations/queen_grey.png")) +black_pawn = black_pawn.astype("float32") +black_pawn = black_pawn.astype("uint8") +print("I_I") +print(black_pawn[0][:10]) +print("I_I") +print(black_pawn[120][200]) +print(np.unique(black_pawn[:, :, :3])) +print("R") +print(np.unique(black_pawn[:, :, 0])) +print("G") +print(np.unique(black_pawn[:, :, 1])) +print("B") +print(np.unique(black_pawn[:, :, 2])) +print("A") +print(np.unique(black_pawn[:, :, 3])) +plt.imshow(black_pawn[:, :, :]) +plt.show() +plt.imshow(black_pawn[:, :, 3], cmap="gray") +plt.show() + +bp = black_pawn.copy() + +""" +Beautification of the Bishop + +for i in range(300, 400): + for j in range(120, 368): + bp[i][j][0] = 255 + bp[i][j][1] = 255 + bp[i][j][2] = 255 + +for i in range(400, 440): + for j in range(124, 368): + bp[i][j][0] = 255 + bp[i][j][1] = 255 + bp[i][j][2] = 255 + +for i in range(440, 500): + for j in range(125+int((i-440) / 2), 368-int((i-440) / 1.5)): + bp[i][j][0] = 255 + bp[i][j][1] = 255 + bp[i][j][2] = 255 + + +for i in range(460, 500): + for j in range(250+int((i-460) / 2), 359-int((i-460) / 1.5)): + bp[i][j][0] = 255 + bp[i][j][1] = 255 + bp[i][j][2] = 255 + +for i in range(500, 519): + for j in range(155+2*(i-500), 330-2*(i-500)): + bp[i][j][0] = 255 + bp[i][j][1] = 255 + bp[i][j][2] = 255 + + +""" +""" +for i, j in zip([479, 480, 481]*3, [138, 138, 138, 139, 139, 130, 140, 140, 140]): + bp[i][j][0] = 255 + bp[i][j][1] = 0 + bp[i][j][2] = 0 +for i, j in zip([480, 481, 482], [141, 141, 142]): + bp[i][j][0] = 255 + bp[i][j][1] = 0 + bp[i][j][2] = 0 +plt.figure() +plt.imshow(bp[:, :, :3]) +plt.show() +""" + +""" +# Beautification of Rook + +for i in range(80, 86): + for j in range(114, 168): + bp[i][j][0] = 120 + bp[i][j][1] = 120 + bp[i][j][2] = 120 +for i in range(42, 86): + for j in range(114, 120): + bp[i][j][0] = 120 + bp[i][j][1] = 120 + bp[i][j][2] = 120 +for i in range(42, 86): + for j in range(162, 168): + bp[i][j][0] = 120 + bp[i][j][1] = 120 + bp[i][j][2] = 120 + +for i in range(80, 86): + for j in range(205, 259): + bp[i][j][0] = 120 + bp[i][j][1] = 120 + bp[i][j][2] = 120 + +for i in range(86, 90): + for j in range(205, 260): + bp[i][j][0] = 100 + bp[i][j][1] = 100 + bp[i][j][2] = 100 + +for i in range(42, 86): + for j in range(205, 211): + bp[i][j][0] = 120 + bp[i][j][1] = 120 + bp[i][j][2] = 120 +for i in range(42, 86): + for j in range(253, 259): + bp[i][j][0] = 120 + bp[i][j][1] = 120 + bp[i][j][2] = 120 +""" + +plt.figure() +plt.imshow(bp[:, :, :3]) +plt.show() + +black_pawn = bp + +black_pawn = black_pawn[:, :, :3] +for h in range(len(black_pawn)): + row = black_pawn[h] + for w in range(len(row)): + pix = row[w] + for i in range(len(pix)): + if pix[i] in list(range(116, 165)): + black_pawn[h][w][i] = 255 + elif pix[i] == 255 or pix[i] == 0: + pix[i] = 0 + else: + pix[i] = 128 * (i == 0) + +plt.imshow(black_pawn[:, :, :3]) +plt.show() + +img = Image.fromarray(black_pawn[:, :, 0]) +img.save("../gs_illustrations/queen_grey.png") +print(black_pawn) + +r_dict = {0: 0, 128: 255, 255: 175} +g_dict = {0: 0, 128: 192, 255: 238} +b_dict = {0: 0, 128: 203, 255: 238} +own_pawn = [ + np.vectorize(r_dict.get)(black_pawn[:, :, 0].astype("uint8")), + np.vectorize(g_dict.get)(black_pawn[:, :, 0]), + np.vectorize(b_dict.get)(black_pawn[:, :, 0]), +] +own_pawn = np.dstack(own_pawn) +rose = 255, 192, 203 +turq = 175, 238, 238 +print(own_pawn) +plt.imshow(own_pawn) +plt.show() diff --git a/utils/create_figures.py b/utils/create_figures.py new file mode 100644 index 0000000..8fb6c3f --- /dev/null +++ b/utils/create_figures.py @@ -0,0 +1,101 @@ +import os + +from PIL import Image +import numpy as np + +color_cell_b = [0, 191, 255] +border_color_b = [0, 0, 0] +inner_color_b = [109, 7, 26] + +color_cell_w = [207, 185, 151] +border_color_w = [255, 255, 255] +inner_color_w = [200, 200, 200] + +down_color_cell_b = np.clip(np.array(color_cell_b) - 70, 0, 255) +down_color_cell_w = np.clip(np.array(color_cell_w) - 70, 0, 255) + + +def color_one_img(pil_img, background_color, border_color, inner_color): + img = np.array(pil_img) + x_min = np.min(np.where(img == 255)[0]) + x_max = np.max(np.where(img == 255)[0]) + img = img[x_min:x_max] + + r_dict = {0: background_color[0], 128: inner_color[0], 255: border_color[0]} + g_dict = {0: background_color[1], 128: inner_color[1], 255: border_color[1]} + b_dict = {0: background_color[2], 128: inner_color[2], 255: border_color[2]} + + img = [ + np.vectorize(r_dict.get)(img), + np.vectorize(g_dict.get)(img), + np.vectorize(b_dict.get)(img), + ] + img = np.dstack(img) + + img = Image.fromarray(img.astype("uint8")) + + img = img.resize((int(img.size[0] / img.size[1] * 450), 450)) + + x_coordinate = int((500 - img.size[0]) / 2) + y_coordinate = 25 + + background = np.dstack( + [np.ones((500, 500)) * background_color[i] for i in range(3)] + ).astype("uint8") + background = Image.fromarray(background) + background.paste(img, (x_coordinate, y_coordinate)) + return background + + +for img_name in os.listdir("../gs_illustrations"): + img = Image.open(os.path.join("../gs_illustrations", img_name)) + img_bb = color_one_img(img, color_cell_b, border_color_b, inner_color_b) + img_ww = color_one_img(img, color_cell_w, border_color_w, inner_color_w) + img_wb = color_one_img(img, color_cell_w, border_color_b, inner_color_b) + img_bw = color_one_img(img, color_cell_b, border_color_w, inner_color_w) + + down_img_bb = color_one_img(img, down_color_cell_b, border_color_b, inner_color_b) + down_img_ww = color_one_img(img, down_color_cell_w, border_color_w, inner_color_w) + down_img_wb = color_one_img(img, down_color_cell_w, border_color_b, inner_color_b) + down_img_bw = color_one_img(img, down_color_cell_b, border_color_w, inner_color_w) + + name = img_name.split("_")[0] + name = { + "pawn": "P", + "bishop": "B", + "knight": "N", + "rook": "R", + "queen": "Q", + "king": "K", + }[name] + img_bb.save(os.path.join("../temp_images", f"bb_{name}.png")) + img_bw.save(os.path.join("../temp_images", f"bw_{name}.png")) + img_wb.save(os.path.join("../temp_images", f"wb_{name}.png")) + img_ww.save(os.path.join("../temp_images", f"ww_{name}.png")) + + down_img_bb.save(os.path.join("../temp_images", f"down_bb_{name}.png")) + down_img_bw.save(os.path.join("../temp_images", f"down_bw_{name}.png")) + down_img_wb.save(os.path.join("../temp_images", f"down_wb_{name}.png")) + down_img_ww.save(os.path.join("../temp_images", f"down_ww_{name}.png")) + +bg_w = Image.fromarray( + np.dstack([np.ones((500, 500)) * color_cell_w[i] for i in range(3)]).astype("uint8") +) +bg_w.save(os.path.join("../temp_images", "w.png")) +bg_b = Image.fromarray( + np.dstack([np.ones((500, 500)) * color_cell_b[i] for i in range(3)]).astype("uint8") +) +bg_b.save(os.path.join("../temp_images", "b.png")) + +down_bg_w = Image.fromarray( + np.dstack([np.ones((500, 500)) * down_color_cell_w[i] for i in range(3)]).astype( + "uint8" + ) +) +down_bg_w.save(os.path.join("../temp_images", "down_w.png")) +down_bg_b = Image.fromarray( + np.dstack([np.ones((500, 500)) * down_color_cell_b[i] for i in range(3)]).astype( + "uint8" + ) +) +down_bg_b.save(os.path.join("../temp_images", "down_b.png"))