0

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)
  • According to the official [document](https://pillow.readthedocs.io/en/stable/reference/ImageTk.html#PIL.ImageTk.PhotoImage): *size – If the first argument is a mode string, this defines the size of the image*. That's why the `size` option does not work in your case. You need to use `Image` class for image resize. – acw1668 Apr 01 '22 at 15:37
  • `PhotoImage` simply does not have the ability to arbitrarily resize images - it can only scale up or down (via `.zoom()` and `.subsample()`) by integer ratios. You can certainly do the job with PIL/Pillow, you'd just need a different function (`.frombuffer()`, I think) to load your data. – jasonharper Apr 01 '22 at 15:40

2 Answers2

0

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)
HelpMePls
  • 1
  • 2
  • thanks for your effort. I know how this method but in order to do this I need to save it from the database which I do not want to do. I want to view it through the database. – Applicator Developer Apr 01 '22 at 20:15
0

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)
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • hi. Thank you for the effort but it did not work. I was hopeful when I first saw your answer however, I remembered that BytesIO does not support this kind of action. Here is the error code: AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo' – Applicator Developer Apr 02 '22 at 21:33
  • It should work if `row[19]` is the raw data of an image. How do you save the image to database? – acw1668 Apr 02 '22 at 22:29
  • it saved the data as memoryview object. – Applicator Developer Apr 03 '22 at 10:17