From 78917db9bc51225d5107186fd7cf1da5b5f532fd Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Thu, 9 Jul 2020 05:46:41 -0400 Subject: [PATCH] New PIL Image Viewer that will also resize images and runs on Qt too --- .../Demo_Image_Elem_Image_Viewer_PIL_Based.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/DemoPrograms/Demo_Image_Elem_Image_Viewer_PIL_Based.py b/DemoPrograms/Demo_Image_Elem_Image_Viewer_PIL_Based.py index 62776c78..686ac6c3 100644 --- a/DemoPrograms/Demo_Image_Elem_Image_Viewer_PIL_Based.py +++ b/DemoPrograms/Demo_Image_Elem_Image_Viewer_PIL_Based.py @@ -13,7 +13,8 @@ import base64 The key to the program is the function "convert_to_bytes" which takes a filename or a bytes object and converts (with optional resize) into a PNG formatted bytes object that - can then be passed to an Image Element's update method + can then be passed to an Image Element's update method. This function can also optionally + resize the image. Copyright 2020 PySimpleGUI.org """ @@ -51,7 +52,8 @@ def convert_to_bytes(file_or_bytes, resize=None): # First the window layout...2 columns left_col = [[sg.Text('Folder'), sg.In(size=(25,1), enable_events=True ,key='-FOLDER-'), sg.FolderBrowse()], - [sg.Listbox(values=[], enable_events=True, size=(40,20),key='-FILE LIST-')]] + [sg.Listbox(values=[], enable_events=True, size=(40,20),key='-FILE LIST-')], + [sg.Text('Resize to'), sg.In(key='-W-', size=(5,1)), sg.In(key='-H-', size=(5,1))]] # For now will only show the name of the file that was chosen images_col = [[sg.Text('You choose from the list:')], @@ -59,7 +61,7 @@ images_col = [[sg.Text('You choose from the list:')], [sg.Image(key='-IMAGE-')]] # ----- Full layout ----- -layout = [[sg.Column(left_col), sg.VSeperator(),sg.Column(images_col, element_justification='c')]] +layout = [[sg.Column(left_col, element_justification='c'), sg.VSeperator(),sg.Column(images_col, element_justification='c')]] # --------------------------------- Create Window --------------------------------- window = sg.Window('Multiple Format Image Viewer', layout,resizable=True) @@ -85,10 +87,13 @@ while True: try: filename = os.path.join(values['-FOLDER-'], values['-FILE LIST-'][0]) window['-TOUT-'].update(filename) - window['-IMAGE-'].update(data=convert_to_bytes(filename)) - except: + if values['-W-'] and values['-H-']: + new_size = int(values['-W-']), int(values['-H-']) + else: + new_size = None + window['-IMAGE-'].update(data=convert_to_bytes(filename, resize=new_size)) + except Exception as E: + print(f'** Error {E} **') pass # something weird happened making the full filename - # --------------------------------- Close & Exit --------------------------------- - window.close()