如何在画布上用 Tkinter 打开 PIL 图像?
Pillow 软件包(或 PIL)极大地帮助处理和加载 Python 项目中的图像。它是一个免费的开源库,可用在 Python 中,可增加加载、处理和处理不同格式图像的支持。
为了在 Tkinter 画布中打开图像,我们必须首先使用 from PIL import Image, ImageTk 命令导入库。为了在我们的 Tkinter 应用程序中显示图像,我们可以通过在 create_image(x,y,image) 方法中指定图像文件来使用画布小部件。
示例
#Import the required Libraries
from tkinter import *
from PIL import Image,ImageTk
#Create an instance of tkinter frame
win = Tk()
#Set the geometry of tkinter frame
win.geometry("750x270")
#Create a canvas
canvas= Canvas(win, width= 600, height= 400)
canvas.pack()
#Load an image in the script
img= ImageTk.PhotoImage(Image.open("download.png"))
#Add image to the Canvas Items
canvas.create_image(10,10,anchor=NW,image=img)
win.mainloop()输出
运行上述代码将在画布小部件内显示一个带有图像的窗口。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP