使用Tkinter通过ImageTk.PhotoImage缩放图像
Tkinter应用程序中,PIL或Pillow库用于处理图像。我们可以使用Pillow来打开图像,调整它们的大小并在窗口中显示。若要调整图像的大小,我们可以使用image_resize((width, height) **options)方法。调整大小的图像之后可以通过标签小部件进行处理和显示。
示例
我们看一下一个示例,其中我们将打开一张图像并调整其大小以便通过标签小部件在窗口中显示。
# Import the required libraries
from tkinter import *
from PIL import Image, ImageTk
# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the tkinter window
win.geometry("700x350")
# Load the image
image=Image.open('download.png')
# Resize the image in the given (width, height)
img=image.resize((450, 350))
# Conver the image in TkImage
my_img=ImageTk.PhotoImage(img)
# Display the image with label
label=Label(win, image=my_img)
label.pack()
win.mainloop()输出
运行以上代码将在窗口中显示调整大小的图像。

Advertisement
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP