Home > Software design >  How to resize the ImageTk.PhotoImage(data=memoryview) without Image.open at first
How to resize the ImageTk.PhotoImage(data=memoryview) without Image.open at first

Time:04-03

I am 100 percent sure that this question is never asked before. I uploaded an image to postgres database. I pulled it to view and currently I am able to view it with this code down below: row[19] is a memoryview. Thats why I was not able to open it with Image.open and resize it.

Additional info: The memoryview looks like this <memory at 0x000001D203D73100>

imge = row[19]
imgd = ImageTk.PhotoImage(data=imge)
image_label.image = imgd
image_label.config(image=imgd)

The problem is I could not manage to resize it. Here are the ways I failed

imge = row[19]
imgd = ImageTk.PhotoImage(data=imge)
imgd = imgd.resize(180,180)
image_label.image = imgd
image_label.config(image=imgd)

I ended up with this: AttributeError: 'PhotoImage' object has no attribute 'resize'

I also tried this, checked this method from its module page. This did not gave an error but nothing changed in the size of the image.

imge = row[19]
imgd = ImageTk.PhotoImage(data=imge, size=(180,180))
image_label.image = imgd
image_label.config(image=imgd)

CodePudding user response:

I think you have to do Image.open

from PIL import Image
i = Image.open("path2")
i.resize((width,height))
img = PIL.ImageTk.PhotoImage(i)

CodePudding user response:

ImageTk.PhotoImage class does not support image resize, you need to use Image class:

from io import BytesIO
from PIL import Image, ImageTk
...

imge = BytesIO(row[19])
imge = Image.open(imge).resize((180,180))
imgd = ImageTk.PhotoImage(imge)
image_label.image = imgd
image_label.config(image=imgd)
  • Related