Displaying an image using PySimpleGUI without having to use an event listener

We Are Going To Discuss About Displaying an image using PySimpleGUI without having to use an event listener. So lets Start this Python Article.

Displaying an image using PySimpleGUI without having to use an event listener

  1. How to solve Displaying an image using PySimpleGUI without having to use an event listener

    Steps to resize and load into sg.Image
    Resize PNG file to size (300, 300) by using PIL.Image
    Convert image to PIL.ImageTk.PhotoImage after window finalized
    update image in sg.Image
    from PIL import Image, ImageTk from urllib import request import PySimpleGUI as sg # Get one PNG file from website and save to file url = ( "https://raw.githubusercontent.com/PySimpleGUI/PySimpleGUI/master/" "images/for_readme/Logo%20with%20text%20for%20GitHub%20Top.png") headers = { 'User-Agent': ( 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' '(KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36')} req = request.Request(url, headers=headers) response = request.urlopen(req) if response.status != 200: print("Failed to load image from website !") exit() data = response.read() filename = "example.png" with open(filename, "wb") as f: f.write(data) # Resize PNG file to size (300, 300) size = (300, 300) im = Image.open(filename) im = im.resize(size, resample=Image.BICUBIC) sg.theme('DarkGreen3') layout = [ [sg.Image(size=(300, 300), key='-IMAGE-')], ] window = sg.Window('Window Title', layout, margins=(0, 0), finalize=True) # Convert im to ImageTk.PhotoImage after window finalized image = ImageTk.PhotoImage(image=im) # update image in sg.Image window['-IMAGE-'].update(data=image) while True: event, values = window.read() if event == sg.WIN_CLOSED: break window.close()

  2. Displaying an image using PySimpleGUI without having to use an event listener

    Steps to resize and load into sg.Image
    Resize PNG file to size (300, 300) by using PIL.Image
    Convert image to PIL.ImageTk.PhotoImage after window finalized
    update image in sg.Image
    from PIL import Image, ImageTk from urllib import request import PySimpleGUI as sg # Get one PNG file from website and save to file url = ( "https://raw.githubusercontent.com/PySimpleGUI/PySimpleGUI/master/" "images/for_readme/Logo%20with%20text%20for%20GitHub%20Top.png") headers = { 'User-Agent': ( 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' '(KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36')} req = request.Request(url, headers=headers) response = request.urlopen(req) if response.status != 200: print("Failed to load image from website !") exit() data = response.read() filename = "example.png" with open(filename, "wb") as f: f.write(data) # Resize PNG file to size (300, 300) size = (300, 300) im = Image.open(filename) im = im.resize(size, resample=Image.BICUBIC) sg.theme('DarkGreen3') layout = [ [sg.Image(size=(300, 300), key='-IMAGE-')], ] window = sg.Window('Window Title', layout, margins=(0, 0), finalize=True) # Convert im to ImageTk.PhotoImage after window finalized image = ImageTk.PhotoImage(image=im) # update image in sg.Image window['-IMAGE-'].update(data=image) while True: event, values = window.read() if event == sg.WIN_CLOSED: break window.close()

Solution 1

Steps to resize and load into sg.Image

  • Resize PNG file to size (300, 300) by using PIL.Image
  • Convert image to PIL.ImageTk.PhotoImage after window finalized
  • update image in sg.Image
from PIL import Image, ImageTk
from urllib import request
import PySimpleGUI as sg

# Get one PNG file from website and save to file
url = (
    "https://raw.githubusercontent.com/PySimpleGUI/PySimpleGUI/master/"
    "images/for_readme/Logo%20with%20text%20for%20GitHub%20Top.png")
headers = {
    'User-Agent': (
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
        '(KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36')}
req = request.Request(url, headers=headers)
response = request.urlopen(req)
if response.status != 200:
    print("Failed to load image from website !")
    exit()
data = response.read()

filename = "example.png"
with open(filename, "wb") as f:
    f.write(data)

# Resize PNG file to size (300, 300)
size = (300, 300)
im = Image.open(filename)
im = im.resize(size, resample=Image.BICUBIC)

sg.theme('DarkGreen3')

layout = [
    [sg.Image(size=(300, 300), key='-IMAGE-')],
]
window = sg.Window('Window Title', layout, margins=(0, 0), finalize=True)

# Convert im to ImageTk.PhotoImage after window finalized
image = ImageTk.PhotoImage(image=im)

# update image in sg.Image
window['-IMAGE-'].update(data=image)

while True:

    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break

window.close()

Original Author Jason Yang Of This Content

Solution 2

sg.Image(size=(300,300),'image_file') 

should be some sort of syntax error. Try

sg.Image('image_file', size=(300,300))

Original Author Justin Ezequiel Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment