display image in pysimplegui code example
Example 1: display image from url in pysimplegui
url = "https://upload.wikimedia.org/wikipedia/commons/d/d9/Test.png"
response = requests.get(url, stream=True)
response.raw.decode_content = True
# img = ImageQt.Image.open(response.raw)
# data = image_to_data(img)
img_box = sg.Image(data=response.raw.read())
Example 2: pysimplegui show image
# image_viewer.py
import io
import os
import PySimpleGUI as sg
from PIL import Image
file_types = [("JPEG (*.jpg)", "*.jpg"),
("All files (*.*)", "*.*")]
def main():
layout = [
[sg.Image(key="-IMAGE-")],
[
sg.Text("Image File"),
sg.Input(size=(25, 1), key="-FILE-"),
sg.FileBrowse(file_types=file_types),
sg.Button("Load Image"),
],
]
window = sg.Window("Image Viewer", layout)
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
if event == "Load Image":
filename = values["-FILE-"]
if os.path.exists(filename):
image = Image.open(values["-FILE-"])
image.thumbnail((400, 400))
bio = io.BytesIO()
image.save(bio, format="PNG")
window["-IMAGE-"].update(data=bio.getvalue())
window.close()
if __name__ == "__main__":
main()