Bam
Bam

Reputation: 1

Problems resizing a PhotoImage on RPi

I'm hoping someone can help with this. I've got a new install of Pi OS (1/12/23) running on an RPi 3. Searching the web, I've found that this is not a new problem but I can't seem to find the answer. My problem is that the following short code runs fine as is.

from tkinter import *
from PIL import Image, ImageTk
root = Tk()
canvas=Canvas(root,width=500, height=300, bg="blue")
canvas.pack(pady=20)
im1 = ImageTk.PhotoImage(Image.open("/home/pi/Downloads/Picture.png"))
#im1 = im1.resize((25,25), Image.ANTIALIAS)
img1=canvas.create_image(0,0, anchor=NW, image=im1)
root.mainloop()

It creates that blue canvas and loads Picture.png just fine. When I remove the comment from the line to resize the image, the blue canvas loads without the Picture.png and I get the error:

im1 = im1.resize((25,25), Image.ANTIALIAS)
AttributeError: 'PhotoImage' object has no attribute 'resize'

I've done all kinds of searches on this and the code I posted is what is recommended when there are obvious errors in the original posted code. For posts that I've seen that use code like this, I've never seen a solution posted but most of them are a few years old so I'll ask. Can someone tell me what is going wrong here?????

Upvotes: 0

Views: 40

Answers (1)

acw1668
acw1668

Reputation: 46707

As the error said resize() is not defined inside class ImageTk.PhotoImage. It is defined in class Image so it should be called on the result of Image.open(...):

im1 = ImageTk.PhotoImage(Image.open("/home/pi/Downloads/Picture.png").resize((25,25)))

Upvotes: 0

Related Questions