单击按钮时显示图像 Python GUI

Display image when button is clicked Python GUI

我正在使用 PySimpleG 开发桌面应用程序UI。

到目前为止,我的 UI 看起来像这样:My UI 如您所见,用户必须单击浏览到 select 图像文件夹,之后该文件夹中所有图像的文件名将显示在其下方的部分中。用户可以单击任何图像。单击任何文件名后,图像将显示在白色 window.

This is how image is selected and displayed on the white window

现在我想要的是当我点击归一化按钮时,原始图像应该被转发到归一化函数并且输出图像应该被归一化图像替换。

def normalise(image, frame):
    equ = cv2.equalizeHist(image)
    equ=update_image(image)
    return equ



def update_listbox(listbox_element, folder, extension, substring):
    path = Path(folder)
    filter_ = substring.lower()
    lst = []
    if folder != '' and path.is_dir():
        files = list(path.glob("*.*"))
        lst = [file for file in files if file.suffix.lower() in extension
            and filter_ in str(file).lower() and file.is_file()]
    listbox_element.update(lst)


def update_image(image_element, filename):
    im = Image.open(filename)
    w, h = size_of_image
    scale = max(im.width/w, im.height/h)
    if scale <= 1:
        image_element.update(filename=filename)
        return im
    else:
        im = im.resize((int(im.width/scale), int(im.height/scale)),
            resample=Image.CUBIC)
        with BytesIO() as output:
            im.save(output, format="PNG")
            im = output.getvalue()
        image_element.update(data=im)

    return ImageTk.PhotoImage(im)


if __name__ == '__main__':


sg.theme('Dark')
sg.set_options(font=('Courier New', 11))

    w, h = size_of_image = (700, 600)

    layout_top = [
        [sg.InputText(enable_events=True, key='-FOLDER-'),
        sg.FolderBrowse('Browse', size=(7, 1), enable_events=True)],
        [sg.InputText(enable_events=True, key='-FILTER-'),
        sg.Button('Search', size=(7, 1))],[sg.Listbox([], size=(45, 10), enable_events=True,
            select_mode=sg.LISTBOX_SELECT_MODE_SINGLE, key='-LISTBOX-')],
    ]

    layout_bottom = [
    [sg.Button('Normalize', size=(20, 1),enable_events=True)],
    [sg.Button('Enhance', size=(20, 1))], [sg.Button('Preprocess', size=(20, 1))],
    [sg.Button('Binarization', size=(20, 1))], [sg.Button('Thinning', size=(20, 1))],
    [sg.Button('Singular Points', size=(20, 1))], [sg.Button('Minutiae', size=(20, 1))],
    [sg.Button('Classify', size=(20, 1))], [sg.Button('Match', size=(20, 1))],
        ]
    layout_left = [
        [sg.Column(layout_top, pad=(0, 0))],
        [sg.Column(layout_bottom, pad=(0, 0))],
    ]

    layout_right = [[sg.Image(background_color='white', key='im')],[sg.Button('NEXT', size= 
   (20, 1),pad=(300,10))]]

    layout = [
        [sg.Column(layout_left), sg.Column(layout_right, pad=(0, 0), size=(w+15, h+15), 
background_color='lightblue', key='-COLUMN-')],

    ]

    window = sg.Window("PNG/GIF Viewer", layout, finalize=True)
    window['im'].Widget.pack(fill='both', expand=True)
    window['im'].Widget.master.pack(fill='both', expand=True)
    window['im'].Widget.master.master.pack(fill='both', expand=True)
    window['-COLUMN-'].Widget.pack_propagate(0)

    while True:
        event, values = window.read()
        if event == sg.WINDOW_CLOSED:
            break
# print(event, values)
        if event in ('-FOLDER-', '-FILTER-', 'Search'):
            update_listbox(window['-LISTBOX-'], values['-FOLDER-'],
                ('.png', '.gif'), values['-FILTER-'])
        elif event == '-LISTBOX-':
            lst = values['-LISTBOX-']
            if lst != []:
                im=update_image(window['im'], values['-LISTBOX-'][0])

                if event == 'normalize':

                    normalise(im, window['im'])


    window.close()

上面的代码在显示原始图像之前工作正常,但我想要的是在显示原始图像之后,函数 returns 图像,当用户点击规范化时,图像被传递到 'normalise function'然后原图被新图替换。

这部分不工作。我不知道我做错了什么。由于我是 python 中 UI 的新手,我发现很难确定我的问题。

if event == 'normalize':
 normalise(im, window['im'])

由于我在colab上写了后端代码,经过测试,后端代码没有报错

为什么event'-LISTBOX-'event'normalize'? 所以语句 `normalize(im, windo['im']) 不会一直执行。

        elif event == '-LISTBOX-':
            lst = values['-LISTBOX-']
            if lst != []:
                im=update_image(window['im'], values['-LISTBOX-'][0])
                if event == 'normalize':
                    normalise(im, window['im'])

不看你代码的细节,也许你的代码应该是这样的,

    im = None
    while True:
        event, values = window.read()
        if event == sg.WINDOW_CLOSED:
            break
        elif event in ('-FOLDER-', '-FILTER-', 'Search'):
            update_listbox(window['-LISTBOX-'], values['-FOLDER-'],
                ('.png', '.gif'), values['-FILTER-'])
        elif event == '-LISTBOX-':
            lst = values['-LISTBOX-']
            if lst != []:
                im=update_image(window['im'], values['-LISTBOX-'][0])
        elif event == 'normalize':
            if im:
                normalise(im, window['im'])
    window.close()