Python 在 Tkinter 中调整 PIL 中的图片大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4066202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resizing pictures in PIL in Tkinter
提问by rectangletangle
I'm currently using PIL to display images in Tkinter. I'd like to temporarily resize these images so that they can be viewed more easily. How can I go about this?
我目前正在使用 PIL 在 Tkinter 中显示图像。我想暂时调整这些图像的大小,以便可以更轻松地查看它们。我该怎么办?
Snippet:
片段:
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)
采纳答案by Joshkunz
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 tkFont
from 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 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 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
这里是 PhotoImage 类文档: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?
注意...在检查了 ImageTK 的 PhotoImage 类(非常稀疏)上的 pythonware 文档后,我似乎认为,如果您的代码段有效,那么只要您导入 PIL“Image”库和 PIL“ImageTK”库和PIL 和 tkinter 都是最新的。另一方面,我什至找不到适合我生活的“ImageTK”模块生活。你能发布你的进口吗?
回答by Bryan Oakley
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 copymethod which lets you zoom or subsample the original image when making the copy. Unfortunately it only lets you zoom/subsample in factors of 2.
最简单的方法可能是基于原始图像创建一个新图像,然后用较大的副本替换原始图像。为此,tk 图像有一种copy方法可以让您在制作副本时缩放或对原始图像进行二次采样。不幸的是,它只能让您以 2 的因子缩放/子采样。
回答by ChaosPredictor
if you don't want save it you can try it:
如果你不想保存它,你可以试试:
from Tkinter import *
from PIL import Image, ImageTk
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()

