Resizing pictures in PIL in Tkinter Resizing pictures in PIL in Tkinter tkinter tkinter

Resizing pictures in PIL in Tkinter


Here's what I do and it works pretty well...

image = Image.open(Image_Location)image = image.resize((250, 250), Image.ANTIALIAS) ## The (250, 250) is (height, width)self.pw.pic = ImageTk.PhotoImage(image)

There you go :)

EDIT:

Here is my import statement:

from Tkinter import *import tkFontfrom PIL import Image

And here is the complete working code I adapted this example from:

im_temp = Image.open(Image_Location)im_temp = im_temp.resize((250, 250), Image.ANTIALIAS)im_temp.save("ArtWrk.ppm", "ppm") ## The only reason I included this was to convert## The image into a format that Tkinter woulden't complain aboutself.photo = PhotoImage(file="ArtWrk.ppm") ## Open the image as a tkinter.PhotoImage class()self.Artwork.destroy() ## Erase the last drawn picture (in the program the picture I used was changing)self.Artwork = Label(self.frame, image=self.photo) ## Sets the image too the labelself.Artwork.photo = self.photo ## Make the image actually display (If I don't include this it won't display an image)self.Artwork.pack() ## Repack the image

And here are the PhotoImage class docs: http://www.pythonware.com/library/tkinter/introduction/photoimage.htm

Note...After checking the pythonware documentation on ImageTK's PhotoImage class (Which is very sparse) I appears that if your snippet works than this should as well as long as you import the PIL "Image" Library an the PIL "ImageTK" Library and that both PIL and tkinter are up-to-date. On another side-note I can't even find the "ImageTK" module life for the life of me. Could you post your imports?


if you don't want save it you can try it:

from Tkinter import *from PIL import Image, ImageTkroot = Tk()same = True#n can't be zero, recommend 0.25-4n=2path = "../img/Stalin.jpeg" image = Image.open(path)[imageSizeWidth, imageSizeHeight] = image.sizenewImageSizeWidth = int(imageSizeWidth*n)if same:    newImageSizeHeight = int(imageSizeHeight*n) else:    newImageSizeHeight = int(imageSizeHeight/n) image = image.resize((newImageSizeWidth, newImageSizeHeight), Image.ANTIALIAS)img = ImageTk.PhotoImage(image)Canvas1 = Canvas(root)Canvas1.create_image(newImageSizeWidth/2,newImageSizeHeight/2,image = img)      Canvas1.config(bg="blue",width = newImageSizeWidth, height = newImageSizeHeight)Canvas1.pack(side=LEFT,expand=True,fill=BOTH)root.mainloop()


the easiest might be to create a new image based on the original, then swap out the original with the larger copy. For that, a tk image has a copy method which lets you zoom or subsample the original image when making the copy. Unfortunately it only lets you zoom/subsample in factors of 2.