有人能帮助我如何使用ImageTk调整图像的大小吗?
我有一张画布,我会把画放在那里。
我有各种各样的照片
当我将图片(只有一张)附加到画布上时,我希望调整图片的大小,使其适合画布,并且仍然保持其比例。
请帮帮我!我是PIL、Tkinter和Python的新手。
更新:
我尝试在Image
下使用thumbnail
,但在调整大小时:
self.image.thumbnail(self.picsize,Image.ANTIALIAS)
图像与画布大小不匹配,如果图像比画布长/宽,则仅进行剪切。(调整大小以适应画布)
代码:
from PIL import ImageTk
from Tkinter import *
import os,tkFileDialog,Image
picsize = 250,250 # I want to set this so that it will fit in the self.imagecanvas | Every images attached will share same Size
imagepath = "Default_ProPic.jpg"
class GUI():
global picsize
def display(self):
self.canvas = Canvas(width=1200,height=700)
self.canvas.pack()
self.imagecanvas = Canvas(self.canvas,width=400,height=400)
self.imagecanvas.place(x=980,y=180)
self.image = Image.open(imagepath)
self.image.thumbnail(picsize,Image.ANTIALIAS)
self.newimage = ImageTk.PhotoImage(self.image)
self.profile_picture=self.imagecanvas.create_image(0,0,anchor = NW,image=self.newimage)
attachbutton = Button(self.canvas,text=" Profile Pic ",command=lambda:self.attachpic())
attachbutton.place(x=1030,y=320)
mainloop()
def attachpic(self):
global picsize
attachphoto = tkFileDialog.askopenfilename(title="Attach photo")
self.image = Image.open(attachphoto)
self.image.thumbnail(picsize,Image.ANTIALIAS)
self.newimage = ImageTk.PhotoImage(self.image)
self.imagecanvas.itemconfigure(self.profile_picture, image=self.newimage)
GUI = GUI()
GUI.display()
上面使用的图片:
尝试将缩略图另存为单独的变量:
我怀疑它可能拿走了原始的
self.image = Image.open(attachphoto)
我建议你看看尺码是多少:
检查输出大小,并验证它是否与原始和所需的(250,250)缩略图相同。
相关问题 更多 >
编程相关推荐