window["image3"].update(data=out_img,visible=True) File…
- User
window["image3"].update(data=out_img,visible=True) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/PySimpleGUI/PySimpleGUI.py", line 5547, in update width, height = size[0] if size[0] is not None else image.width(), size[1] if size[1] is not None else image.height() AttributeError: 'numpy.ndarray' object has no attribute 'width'
- Assistant
It looks like the "out_img" variable is a numpy array, but the PySimpleGUI's Image element is expecting a PIL.Image object. You can use the "PIL.ImageTk.PhotoImage" class to convert the numpy array to a format that works with PySimpleGUI.
First, make sure you have installed the "Pillow" library, which provides the PIL (Python Imaging Library) package:
bashpip install PillowNow, modify your code like this:
pythonimport PySimpleGUI as sg from PIL import Image, ImageTk import numpy as np # Assuming out_img is a numpy array with the image data out_img = out_img.astype('uint8') # Make sure the data type is unsigned 8-bit integers # Convert the numpy array to a PIL.Image object out_img = Image.fromarray(out_img) # Convert the PIL.Image object to a PhotoImage object compatible with PySimpleGUI out_img = ImageTk.PhotoImage(out_img) # Now you can update the image element using the out_img variable window["image3"].update(data=out_img, visible=True)