Rthirteen
Rthirteen

Reputation: 1

PySimpleGUI: How to select an image from file browser and save to another folder

Is there a simple way to select an image from the file browser, view it, and then move it to another folder using PySimpleGUI? I have the code for selecting an image but I don't know how to view it and move to another folder.

import PySimpleGUI as sg
import os
from PIL import Image, ImageTk
import io
import sys

# Get the file for test image
if len(sys.argv)==1:
    fname = sg.popup_get_file('Select Image To Use as TestImage', default_path='')
else:
    fname = sys.argv[1]
    
if not fname:
    sg.popup_cancel('Cancelling')
    raise SystemExit()
else:
    sg.popup('The file chosen is', fname)

This is the code for selecting an image.

How can I view in my UI and move the selected image to another folder. Sort of like uploading an image and storing it on a server(like in social media platforms).

Upvotes: 0

Views: 1513

Answers (1)

Jason Yang
Jason Yang

Reputation: 13057

Using method popup_get_file to get filename to open, and popup_get_folder to get which directory to move file to.

Using Image element to show the image, maybe also library PIL.Image to fit the size of image.

Demo code for it, here not specified the default path when browse file or browse directory.

from pathlib import Path
from io import BytesIO
from PIL import Image
import PySimpleGUI as sg


def update_image(image_element, filename):
    if not Path(filename).is_file():
        return
    try:
        im = Image.open(filename)
    except:
        return
    width, height = size_of_image
    scale = max(im.width/width, im.height/height)

    if scale > 1:
        w, h = int(im.width/scale), int(im.height/scale)
        im = im.resize((w, h), resample=Image.CUBIC)

    with BytesIO() as output:
        im.save(output, format="PNG")
        data = output.getvalue()
    image_element.update(data=data)

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

w, h = size_of_image = (600, 600)

layout = [
    [sg.Button('Open'), sg.Button('Move'), sg.Input(expand_x=True, disabled=True, key='Filename')],
    [sg.Image(background_color='green', size=size_of_image, key='-IMAGE-', expand_x=True, expand_y=True)],
]

window = sg.Window("Image Viewer", layout, size=(800, 800), enable_close_attempted_event=True, finalize=True)
filename = None

while True:

    event, values = window.read()
    if event in (sg.WINDOW_CLOSE_ATTEMPTED_EVENT, "Exit"):
        break
    elif event == 'Open':
        filename = sg.popup_get_file("", no_window=True)
        if filename:
            update_image(window['-IMAGE-'], filename)
            window['Filename'].update(filename)
    elif event == 'Move':
        if filename and Path(filename).is_file():
            old_directory = Path(filename).parent
            directory = sg.popup_get_folder("", no_window=True)
            if directory and Path(directory).is_dir():
                if Path(directory) != old_directory:
                    new_path = Path(directory).joinpath(Path(filename).name)
                    Path(filename).replace(new_path)
                    filename = str(new_path)
                    window['Filename'].update(filename)

window.close()

Upvotes: 1

Related Questions