[Python] Python Script/Programm to Search & Display an Image from Local Folder

Discussão em 'Python' iniciado por Stack, Setembro 2, 2025.

  1. Stack

    Stack Membro Participativo

    I'm an absolute beginner in python and trying to build a script/programm that searches images from a folder by their name (meaning that it would need a search bar) and displays these image in the same window.

    Think of it as sort of a phonebook where I have stored 100 names and pictures and I want to type in "Henry" to see Henry's picture.

    On my search for a software that achieves this, I've come accross something similar:

    # img_viewer.py

    import PySimpleGUI as sg
    import os.path

    # First the window layout in 2 columns

    file_list_column = [
    [
    sg.Text("Image 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-"
    )
    ],
    ]

    # For now will only show the name of the file that was chosen
    image_viewer_column = [
    [sg.Text("Choose an image from list on left:")],
    [sg.Text(size=(40, 1), key="-TOUT-")],
    [sg.Image(key="-IMAGE-")],
    ]

    # ----- Full layout -----
    layout = [
    [
    sg.Column(file_list_column),
    sg.VSeperator(),
    sg.Column(image_viewer_column),
    ]
    ]

    window = sg.Window("Image Viewer", layout)

    # Run the Event Loop
    while True:
    event, values = window.read()
    if event == "Exit" or event == sg.WIN_CLOSED:
    break
    # Folder name was filled in, make a list of files in the folder
    if event == "-FOLDER-":
    folder = values["-FOLDER-"]
    try:
    # Get list of files in folder
    file_list = os.listdir(folder)
    except:
    file_list = []

    fnames = [
    f
    for f in file_list
    if os.path.isfile(os.path.join(folder, f))
    and f.lower().endswith((".png", ".gif"))
    ]
    window["-FILE LIST-"].update(fnames)
    elif event == "-FILE LIST-": # A file was chosen from the listbox
    try:
    filename = os.path.join(
    values["-FOLDER-"], values["-FILE LIST-"][0]
    )
    window["-TOUT-"].update(filename)
    window["-IMAGE-"].update(filename=filename)

    except:
    pass

    window.close()


    This is almost perfect, but I would need to implement a search bar instead of clicking through the names as I will have ~20.000 entries and clicking through them is just not viable.

    Any idea how I would go about achieving this?

    Continue reading...

Compartilhe esta Página