alessandro alessandro - 1 year ago 102
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)
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 =
img = img.resize((w,h), Image.ANTIALIAS)
photoImg = ImageTk.PhotoImage(img)
text.image_create(INSERT, image=photoImg)

thank you!

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download