2010-11-01 94 views
10

Actualmente estoy usando PIL para mostrar imágenes en Tkinter. Me gustaría cambiar el tamaño temporal de estas imágenes para que se puedan ver más fácilmente. ¿Cómo puedo hacer esto?Cambiar el tamaño de las imágenes en PIL en Tkinter

Fragmento:

self.pw.pic = ImageTk.PhotoImage(Image.open(self.pic_file)) 
self.pw.pic_label = TK.Label(self.pw , image=self.pw.pic,borderwidth=0)   
self.pw.pic_label.grid(column=0,row=0) 

Respuesta

20

Aquí es lo que hago y funciona bastante bien ...

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

hay que ir :)

EDIT:

Aquí es mi estado de importación:

from Tkinter import * 
import tkFont 
import Image #This is the PIL Image library 

Y aquí está el código completo de trabajo me he adaptado este ejemplo a partir de:

im_temp = Image.open(Path-To-Photo) 
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 about 
self.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 label 
self.Artwork.photo = self.photo ##Make the image actually display (If I dont include this it won't display an image) 
self.Artwork.pack() ##repack the image 

Y aquí están los documentos de clase PhotoImage: http://www.pythonware.com/library/tkinter/introduction/photoimage.htm

Nota ... Después de comprobar la documentación pythonware en PhotoImage de ImageTK clase (Que es muy escasa) Parece que si su fragmento funciona más de lo debido, siempre y cuando importe la Biblioteca de "Imagen" de PIL y la Biblioteca "ImageTK" de PIL y que tanto PIL como tkinter estén actualizados. Por otro lado, ni siquiera puedo encontrar la vida útil del módulo "ImageTK". ¿Podrías publicar tus importaciones?

+2

me siguen dando este "AttributeError: PhotoImage instancia no tiene atributo 'redimensionar '". ¿Qué necesito para importar? – rectangletangle

+0

@ Anteater7171 Se incluye algo más de información – Joshkunz

+0

Es (ancho, alto), no (alto, ancho). – Jacob

1

lo más fácil podría ser crear una nueva imagen basada en el original, luego cambiar el original con la copia más grande. Para eso, una imagen tk tiene un método copy que le permite acercar o submuestrear la imagen original al hacer la copia. Por desgracia, sólo se le permite hacer zoom/submuestra en factores de 2.

3

si no desea guardarlo se puede probar:

from Tkinter import * 
import ImageTk 
import Image 

root = Tk() 

same = True 
#n can't be zero, recommend 0.25-4 
n=2 

path = "../img/Stalin.jpeg" 
image = Image.open(path) 
[imageSizeWidth, imageSizeHeight] = image.size 

newImageSizeWidth = 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() 
Cuestiones relacionadas