你能用Python Tkinter重新缩放照片吗?
在我的程序中,我有两个按钮你能用Python Tkinter重新缩放照片吗?,python,tkinter,Python,Tkinter,在我的程序中,我有两个按钮button1调用一个函数(bt1),该函数在窗口中显示一个图像,然后删除两个按钮按钮2调用函数(bt2)我希望此按钮显示与按钮1相同的图像,但比例因子为一半。我是这样想的: scale_half_img = PhotoImage(file = 'Image.png', scale = 0.5) 当然,这不起作用,但这是我正在寻找的东西 完整代码: from tkinter import * window = Tk() def bt1(): img = Ph
button1
调用一个函数(bt1
),该函数在窗口中显示一个图像,然后删除两个按钮<代码>按钮2调用函数(bt2
)我希望此按钮显示与按钮1相同的图像,但比例因子为一半。我是这样想的:
scale_half_img = PhotoImage(file = 'Image.png', scale = 0.5)
当然,这不起作用,但这是我正在寻找的东西
完整代码:
from tkinter import *
window = Tk()
def bt1():
img = PhotoImage(file = 'Image.png')
imglbl = Label(window, image = img, anchor = 'nw')
imglbl.place(x = 0, y = 0, width = 865, height = 800)
button1.destroy()
button2.destroy()
def bt2():
# display scaled down image
button1.destroy()
button2.destroy()
button1 = Button(window, text = 'Display Image', command = bt1)
button1.place(x = 10, y = 10, width = 200, height = 30)
button2 = Button (window, text = 'Display Rescaled Image', command = bt2)
button2.place(x = 10, y = 50, width = 200, height = 30)
window.mainloop()
使用
PIL.Image
在tkinter中调整图像大小。您可以按如下方式进行操作:
from PIL import Image, ImageTk
img = Image.open('<Path to Image>')
img = img.resize((img.size[0]/2, img.size[1]/2), Image.ANTIALIAS)
resized_image = ImageTk.Photoimage(img) # use this
从PIL导入图像,ImageTk
img=图像打开(“”)
img=img.resize((img.size[0]/2,img.size[1]/2),Image.antialas)
调整大小的_image=ImageTk.Photoimage(img)#使用此
您不能单独使用tkinter,还需要使用PIL模块。@martineau:这不完全正确。Tkinter PhotoImage能够通过使用子样本
和缩放
选项,通过复制
方法移除或复制像素,从而上下缩放。