Image 调整按钮上的图像
如何将图像调整为Tkinter中的按钮 其实我有这个,Image 调整按钮上的图像,image,python-2.7,button,tkinter,Image,Python 2.7,Button,Tkinter,如何将图像调整为Tkinter中的按钮 其实我有这个, originalImg = Image.open(currentphotofolderPath + file) img = ImageTk.PhotoImage(originalImg) Button(photoFrame, image = img, borderwidth=0, height = 200, width = 200) 问题是图像无法调整到200x200的按钮 我不想使用PhotoImage.resize()调整图像大小该函
originalImg = Image.open(currentphotofolderPath + file)
img = ImageTk.PhotoImage(originalImg)
Button(photoFrame, image = img, borderwidth=0, height = 200, width = 200)
问题是图像无法调整到200x200的按钮
我不想使用PhotoImage.resize()
调整图像大小该函数应该可以解决您的问题:
返回与此小部件具有相同图像的新照片图像,但缩放它
用X和Y
在实例化按钮()
小部件之前添加下面的代码行应该会有所帮助:
originalImg = Image.open(currentphotofolderPath + file)
originalImg.zoom(200, 200)
img = ImageTk.PhotoImage(originalImg)
Button(photoFrame, image=img, borderwidth=0, height=200, width=200)
您有两个选择,一个是Billal发布的缩放功能,另一个是创建调整大小功能:
def Resize_Image(image, maxsize):
r1 = image.size[0]/maxsize[0] # width ratio
r2 = image.size[1]/maxsize[1] # height ratio
ratio = max(r1, r2)
newsize = (int(image.size[0]/ratio), int(image.size[1]/ratio))
image = image.resize(newsize, Image.ANTIALIAS)
return image
然后将图像(而不是照片图像)调整为最大可能的大小,同时保留纵横比(事先不知道)
请注意,调整大小方法使用的内存应该比缩放方法少(如果这是一个重要因素)您必须首先调整图像大小,按钮无法为您调整图像大小,手动设置其大小将有效地只裁剪图像的一部分。您必须使用
image.resize
或PhotoImage.resize
Ok来调整图像大小。。。因此,我们可以调整图像大小但保留属性,让我解释一下:例如,我的图像是300x600,我不想将其设置为200x200,而是设置为100x200。问题是我不知道我的图像的尺寸…回复的Thx我的代码有一个错误:Traceback(最后一次调用):文件“P:/projet-python-photo/Sources/GUI/main.py”,第180行,在originalImg.zoom(200200)文件“C:\Python27\lib\site-packages\PIL\image.py”,第512行,在\uu getattr\uuuuu\raise AttributeError中(名称)AttributeError:zoom
这是我从Tkinter导入的导入,来自PIL导入图像,ImageTk