From e3185a22aee88436392b810b09ae703aaa065088 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Tue, 16 Jun 2020 07:58:24 -0400 Subject: [PATCH] Fix for image resizing algorithm. Was only resizing to square images. --- DemoPrograms/Demo_Graph_Elem_Image_Album.py | 36 +++++++++------------ 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/DemoPrograms/Demo_Graph_Elem_Image_Album.py b/DemoPrograms/Demo_Graph_Elem_Image_Album.py index 2e4e994b..aadabd38 100644 --- a/DemoPrograms/Demo_Graph_Elem_Image_Album.py +++ b/DemoPrograms/Demo_Graph_Elem_Image_Album.py @@ -16,38 +16,34 @@ import os Copyright 2020 PySimpleGUI.org """ -G_SIZE = (400,400) +G_SIZE = (800,600) def get_img_filename(f, resize=None): - """Generate image data using PIL + """ + Resizes an image file and returns the result as a byte string which can be used to update a PySimpleGUI Element """ img = PIL.Image.open(f) - cur_width, cur_height = img.size - if resize: - new_width, new_height = resize - if cur_width > cur_height: - new_height = int(new_height * cur_height/cur_width) - else: - new_width = int(new_width * cur_width/cur_height) - img = img.resize((new_width, new_height), PIL.Image.ANTIALIAS) - bio = io.BytesIO() - img.save(bio, format="PNG") - del img - return bio.getvalue() + return resize_pil_image(img, resize) def get_img_data(data, resize=None): """Generate PIL.Image data using PIL """ img = PIL.Image.open(io.BytesIO(base64.b64decode(data))) + return resize_pil_image(img, resize) + +def resize_pil_image(img, resize=None): + """ + Resizes a PIL image to a new size or just converts to PNG as a byte string + :param img: + :param resize: + :return: + """ cur_width, cur_height = img.size if resize: new_width, new_height = resize - if cur_width > cur_height: - new_height = int(new_height * cur_height/cur_width) - else: - new_width = int(new_width * cur_width/cur_height) - img = img.resize((new_width, new_height), PIL.Image.ANTIALIAS) + scale = min(new_height/cur_height, new_width/cur_width) + img = img.resize((int(cur_width*scale), int(cur_height*scale)), PIL.Image.ANTIALIAS) bio = io.BytesIO() img.save(bio, format="PNG") del img @@ -59,7 +55,7 @@ if not folder: file_list = os.listdir(folder) fnames = [f for f in file_list if os.path.isfile( - os.path.join(folder, f)) and f.lower().endswith((".png", ".jpg", "jpeg", ".tiff", ".bmp"))] + os.path.join(folder, f)) and f.lower().endswith((".png", ".jpg", "jpeg", ".tiff", ".bmp", ".gif", ".ico"))] num_files = len(fnames) graph = sg.Graph(canvas_size=G_SIZE, graph_bottom_left=(0, 0), graph_top_right=G_SIZE, enable_events=True, key='-GRAPH-')