alessandro alessandro - 14 days ago 8
Python Question

image resize under PhotoImage

I need to resize an image, but I want to avoid PIL, since I cannot make it work under osx - don't ask me why...

Anyway since I am satisfied with gif/pgm/ppm, the PhotoImage class is ok for me:

photoImg = PhotoImage(file=imgfn)
images.append(photoImg)
text.image_create(INSERT, image=photoImg)


The problem is - how do I resize the image?
The following works only with PIL, which is the non-PIL equivalent?

img = Image.open(imgfn)
img = img.resize((w,h), Image.ANTIALIAS)
photoImg = ImageTk.PhotoImage(img)
images.append(photoImg)
text.image_create(INSERT, image=photoImg)


thank you!
alessandro

Answer

You have to either use the subsample() or the zoom() methods of the PhotoImage class. For the first option you first have to calculate the scale factors, simply explained in the following lines:

scale_w = new_width/old_width
scale_h = new_height/old_height
photoImg.zoom(scale_w, scale_h)