From e106370970e83d1fa8fa27ee149f4cadeaf3a7e2 Mon Sep 17 00:00:00 2001 From: Aur Saraf Date: Fri, 6 May 2022 16:24:50 +0300 Subject: [PATCH] use io.BytesIO instead of writing images to disk --- draw.py | 13 +++++++++---- hanabi.py | 4 +++- main.py | 13 ++++--------- todo.md | 1 - 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/draw.py b/draw.py index 4618d44..5f8c1f1 100644 --- a/draw.py +++ b/draw.py @@ -1,5 +1,6 @@ import hanabi from PIL import Image, ImageDraw, ImageFont +import io def rounded_rectangle(image: ImageDraw, xy, corner_radius, fill=None, outline=None): upper_left_point = xy[0] @@ -77,7 +78,7 @@ def render_card_friend(image, x, y, color, value): # if color == 'black': text_fill = (255, 255, 255) image.text((x + width/2.5, y + height/8), value, font=text_font, fill=text_fill) -def draw_board_state(game, player_viewing, filename): +def draw_board_state(game, player_viewing): width = int(400 / size) height = (width * 16) // 9 if len(game.players) == 4: @@ -196,8 +197,10 @@ def draw_board_state(game, player_viewing, filename): if len(game.players) < 4: y -= 50/size else: y -= 40/size draw.text((x, y), game.last_action_description, font=text_font, fill=text_fill) - - image.save(filename) + image_file = io.BytesIO() + image.save(image_file, "PNG") + image_file.seek(0) + return image_file if __name__ == '__main__': game = hanabi.Game(['Giacomo', 'Gabriele', 'Fabrizio']) @@ -213,4 +216,6 @@ def draw_board_state(game, player_viewing, filename): # game.hands['Giacomo'][0].is_value_known = True # game.hands['Giacomo'][0].not_values = [1, 2, 3] # game.hands['Giacomo'][0].not_colors = ['red', 'blue', 'green'] - draw_board_state(game, 'Giacomo', 'image.png') + image = draw_board_state(game, 'Giacomo') + with open('image.png', 'wb') as f: + f.write(image.read()) diff --git a/hanabi.py b/hanabi.py index f34f598..507b9d5 100644 --- a/hanabi.py +++ b/hanabi.py @@ -398,7 +398,9 @@ def main(): while True: # print_board_state(game, game.players[game.active_player]) - draw.draw_board_state(game, game.players[game.active_player], 'image.png') + image = draw.draw_board_state(game, game.players[game.active_player]) + with open('image.png', 'wb') as f: + f.write(image.read()) result = check_state(game) if result > 0: diff --git a/main.py b/main.py index b1c6df3..c108aab 100644 --- a/main.py +++ b/main.py @@ -54,12 +54,9 @@ def add_player(server, chat_id, user_id, name, allow_repeated_players=False): def send_game_views(bot, chat_game, last_player=''): for name, user_id in chat_game.player_to_user.items(): - # TODO: Send directly generated image, without write to disk. - filename = str(user_id) + '.png' - draw.draw_board_state(chat_game.game, name, filename) + image = draw.draw_board_state(chat_game.game, name) try: - with open(filename, 'rb') as image: - bot.sendPhoto(user_id, image) + bot.sendPhoto(user_id, image) except Exception as ex: print(ex) pass @@ -173,11 +170,9 @@ def handle_game_ending(bot, chat_game): send_game_views(bot, chat_game) chat_id = chat_game.chat_id game = chat_game.game - filename = str(chat_id) + '.png' - draw.draw_board_state(chat_game.game, '', filename) + image = draw.draw_board_state(chat_game.game) try: - with open(filename, 'rb') as image: - bot.sendPhoto(chat_id, image) + bot.sendPhoto(chat_id, image) except Exception as ex: print(ex) diff --git a/todo.md b/todo.md index fdc6890..87fe384 100644 --- a/todo.md +++ b/todo.md @@ -1,7 +1,6 @@ # Todo ## Chatbot -- Send image without writing to disk. - Dump memory to disk in order to resurrect games after crashes or disconnections ## Rendering