|
22 | 22 | import PySimpleGUI as sg
|
23 | 23 | import cv2
|
24 | 24 |
|
| 25 | +def process_and_view_image(): |
| 26 | + '''Process and display the image.''' |
| 27 | + try: |
| 28 | + file_name = os.path.join( |
| 29 | + VALUES["-FOLDER-"], VALUES["-FILE LIST-"][0] |
| 30 | + ) |
| 31 | + WINDOW["-IMAGE_FILE-"].update(file_name) |
| 32 | + image = cv2.imread(file_name) # pylint: disable=no-member |
| 33 | + |
| 34 | + # Resize the image if the dimension is to large. |
| 35 | + if image.shape[0] > 999 and image.shape[1] > 768: |
| 36 | + image = cv2.resize(image, (1024, 768), interpolation=cv2.INTER_NEAREST) # pylint: disable=no-member |
| 37 | + |
| 38 | + WINDOW["-IMAGE-"].update(data=cv2.imencode('.png', image)[1].tobytes()) # pylint: disable=no-member |
| 39 | + # WINDOW["-IMAGE-"].update(filename=FILENAME) |
| 40 | + WINDOW["-STATIC_TEXT-"].update("Image Selected:", font=("Helvetica", 12)) |
| 41 | + |
| 42 | + except: # pylint: disable=bare-except |
| 43 | + pass |
| 44 | + |
| 45 | +def up_down_arrow_key_selection(arrow_key_event): |
| 46 | + '''Process the list selection usingt he Up and Down arrow key events. |
| 47 | + More information related to up/down arrow key seletion on a listbox |
| 48 | + can be viewed @ https://github.com/PySimpleGUI/PySimpleGUI/issues/1629''' |
| 49 | + |
| 50 | + try: |
| 51 | + current_index = WINDOW.Element('-FILE LIST-').Widget.curselection() |
| 52 | + |
| 53 | + if arrow_key_event == 'Up:38': |
| 54 | + current_index = (current_index[0] - 1) % WINDOW.Element('-FILE LIST-').Widget.size() |
| 55 | + elif arrow_key_event == 'Down:40': |
| 56 | + current_index = (current_index[0] + 1) % WINDOW.Element('-FILE LIST-').Widget.size() |
| 57 | + |
| 58 | + WINDOW.Element('-FILE LIST-').Update(set_to_index=current_index) |
| 59 | + WINDOW.Element('-FILE LIST-').Update(scroll_to_index=current_index) |
| 60 | + WINDOW.write_event_value('-FILE LIST-', |
| 61 | + [WINDOW.Element('-FILE LIST-').GetListValues()[current_index]]) |
| 62 | + except: # pylint: disable=bare-except |
| 63 | + pass |
| 64 | + |
25 | 65 | sg.theme('dark grey 9')
|
26 | 66 |
|
27 | 67 | FILE_SELECT_COLUMN_LAYOUT = [
|
|
30 | 70 | readonly=True, disabled_readonly_background_color='#40444B'),
|
31 | 71 | sg.FolderBrowse(tooltip="Select a folder", key='-FOLDER_BROWSE-'),],
|
32 | 72 | [sg.Text("Images Retrieved:")],
|
33 |
| - [sg.Listbox(values=[], enable_events=True, size=(80, 45), key="-FILE LIST-")], |
| 73 | + [sg.Listbox(values=[], enable_events=True, size=(80, 45), |
| 74 | + key="-FILE LIST-", bind_return_key=True)], |
34 | 75 | [sg.Button("Reset", key="-RESET-"), sg.Button("Exit", key="-Exit-")],]
|
35 | 76 |
|
36 | 77 | IMAGE_VIEWER_COLUMN_LAYOUT = [
|
|
48 | 89 | CURRENT_WORKING_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
|
49 | 90 | WINDOW = sg.Window("Py Image Viewer", WINDOW_LAYOUT, margins=(0, 0),
|
50 | 91 | icon=CURRENT_WORKING_DIRECTORY + "\\img_view.ico",
|
51 |
| - resizable=False, finalize=True) |
| 92 | + resizable=False, finalize=True, return_keyboard_events=True) |
52 | 93 |
|
53 | 94 | # Run the Event Loop
|
54 | 95 | while True:
|
|
82 | 123 | WINDOW["-FILE LIST-"].update(FNAMES)
|
83 | 124 |
|
84 | 125 | elif EVENT == "-FILE LIST-": # Display the image selected.
|
85 |
| - try: |
86 |
| - FILENAME = os.path.join( |
87 |
| - VALUES["-FOLDER-"], VALUES["-FILE LIST-"][0] |
88 |
| - ) |
89 |
| - WINDOW["-IMAGE_FILE-"].update(FILENAME) |
90 |
| - IMAGE = cv2.imread(FILENAME) # pylint: disable=no-member |
91 |
| - |
92 |
| - # Resize the image if the dimension is to large. |
93 |
| - if IMAGE.shape[0] > 999 and IMAGE.shape[1] > 768: |
94 |
| - IMAGE = cv2.resize(IMAGE, (1024, 768), interpolation=cv2.INTER_NEAREST) # pylint: disable=no-member |
95 |
| - |
96 |
| - WINDOW["-IMAGE-"].update(data=cv2.imencode('.png', IMAGE)[1].tobytes()) # pylint: disable=no-member |
97 |
| - # WINDOW["-IMAGE-"].update(filename=FILENAME) |
98 |
| - WINDOW["-STATIC_TEXT-"].update("Image Selected:", font=("Helvetica", 12)) |
99 |
| - |
100 |
| - except: # pylint: disable=bare-except |
101 |
| - pass |
| 126 | + process_and_view_image() |
| 127 | + |
| 128 | + elif EVENT == 'Up:38': |
| 129 | + up_down_arrow_key_selection(EVENT) |
| 130 | + process_and_view_image() |
| 131 | + elif EVENT == 'Down:40': |
| 132 | + up_down_arrow_key_selection(EVENT) |
| 133 | + process_and_view_image() |
102 | 134 |
|
103 | 135 | WINDOW.close()
|
0 commit comments