在 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("tutorialspoint.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”的图片。

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