在 Tkinter 的 PIL 中调整图片大小

Python 提供了图像处理的 Pillow 或 PIL 包,用于在应用程序中加载、处理和自定义图像。它有许多属性,如图像颜色、图像字体、调整图像大小、加载图像等。

为了调整应用程序中图像的大小,我们可以使用该resize(width, height)方法。在应用程序中加载图像后可以调用该方法。为了在应用程序中打开图像,我们必须将包导入笔记本中,

from PIL import Image, ImageTk

示范

在下面的示例中,我们将图像的大小调整为“300x225”。

#Import tkinter library
from tkinter import *
from PIL import Image, ImageTk
#Create an instance of tkinter frame
win= Tk()
#Set the Geometry
win.geometry("750x250")
#Open a New Image
image= Image.open("nhooo.png")
#Resize Image using resize function
resized_image= image.resize((300,225), Image.ANTIALIAS)
#Convert the image into PhotoImage
img = ImageTk.PhotoImage(resized_image)
#Create a label for the image
Label(win,image= img).pack(pady=20)
win.mainloop()
输出结果

运行上面的代码将显示一个窗口,其中包含大小为“300x225”的图像。