调整按钮上的图像

时间:2017-10-23 07:43:05

标签: 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()

调整图片大小

2 个答案:

答案 0 :(得分:1)

zoom()功能应解决您的问题:

  

返回一个与此窗口小部件具有相同图像的新PhotoImage,但缩放它   与X和Y。

在实例化Button()小部件之前添加下面的代码行应该会有所帮助:

originalImg = Image.open(currentphotofolderPath + file)
originalImg.zoom(200, 200)
img = ImageTk.PhotoImage(originalImg)    
Button(photoFrame, image=img, borderwidth=0, height=200, width=200)

答案 1 :(得分:0)

您有几个选择,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

然后将图像(不是PhotoImage)的大小调整到尽可能大的尺寸,同时保持纵横比(事先不知道)

请注意,resize方法应使用比zoom方法更少的内存(如果这是一个重要因素)