diff --git a/DemoPrograms/Demo_PIL_Use.py b/DemoPrograms/Demo_PIL_Use.py index 8f753e2f..e10b9700 100644 --- a/DemoPrograms/Demo_PIL_Use.py +++ b/DemoPrograms/Demo_PIL_Use.py @@ -15,6 +15,14 @@ import base64 Copyright 2020 PySimpleGUI.org """ +def make_square(im, min_size=256, fill_color=(0, 0, 0, 0)): + x, y = im.size + size = max(min_size, x, y) + new_im = Image.new('RGBA', (size, size), fill_color) + new_im.paste(im, (int((size - x) / 2), int((size - y) / 2))) + return new_im + + def convert_to_bytes(file_or_bytes, resize=None, fill=False): ''' Will convert into bytes and optionally resize an image that is a file or a base64 bytes object. @@ -43,7 +51,8 @@ def convert_to_bytes(file_or_bytes, resize=None, fill=False): scale = min(new_height / cur_height, new_width / cur_width) img = img.resize((int(cur_width * scale), int(cur_height * scale)), PIL.Image.ANTIALIAS) if fill: - img = make_square(img, THUMBNAIL_SIZE[0]) + if resize is not None: + img = make_square(img, resize[0]) with io.BytesIO() as bio: img.save(bio, format="PNG") del img @@ -61,7 +70,7 @@ def main(): image = sg._random_error_icon() size = (60,60) - image = convert_to_bytes(image, size) + image = convert_to_bytes(image, size, fill=False) layout = [[sg.Button('+', size=(4,2)), sg.Button('-', size=(4,2)), sg.B('Next', size=(4,2)), sg.T(size, size=(10,1), k='-SIZE-')], [sg.Image(data=image, k='-IMAGE-')], @@ -85,7 +94,7 @@ def main(): image = event_window[event].ImageData # Resize image and update the window - image = convert_to_bytes(image, size) + image = convert_to_bytes(image, size, fill=True) window['-IMAGE-'].update(data=image) window['-BUTTON IMAGE-'].update(image_data=image) window['-SIZE-'].update(size) @@ -93,4 +102,4 @@ def main(): if __name__ == '__main__': - main() + main() \ No newline at end of file