📜  使用 PIL 在 Tkinter 中加载图像

📅  最后修改于: 2022-05-13 01:55:10.636000             🧑  作者: Mango

使用 PIL 在 Tkinter 中加载图像

在本文中,我们将学习如何使用 PIL 模块将图像从用户系统加载到 Tkinter 窗口。该程序将打开一个对话框,从任何目录中选择所需的文件并将其显示在 tkinter 窗口中。
安装要求 –
使用此命令安装 Tkinter :

pip install python-tk

使用此命令安装 PIL:

pip install pillow

导入模块 –

Python3
from tkinter import *
 
# loading Python Imaging Library
from PIL import ImageTk, Image
 
# To get the dialog box to open when required
from tkinter import filedialog


Python3
# Create a window
root = Tk()
 
# Set Title as Image Loader
root.title("Image Loader")
 
# Set the resolution of window
root.geometry("550x300 + 300 + 150")
 
# Allow Window to be resizable
root.resizable(width = True, height = True)
 
# Create a button and place it into the window using grid layout
btn = Button(root, text ='open image', command = open_img).grid(
                                        row = 1, columnspan = 4)
root.mainloop()


Python3
def open_img():
    # Select the Imagename  from a folder
    x = openfilename()
 
    # opens the image
    img = Image.open(x)
     
    # resize the image and apply a high-quality down sampling filter
    img = img.resize((250, 250), Image.ANTIALIAS)
 
    # PhotoImage class is used to add image to widgets, icons etc
    img = ImageTk.PhotoImage(img)
  
    # create a label
    panel = Label(root, image = img)
     
    # set the image as img
    panel.image = img
    panel.grid(row = 2)


Python3
def openfilename():
 
    # open file dialog box to select image
    # The dialogue box has a title "Open"
    filename = filedialog.askopenfilename(title ='"pen')
    return filename


注意: ImageTk 模块支持从 PIL 图像创建和修改 Tkinter BitmapImage 和 PhotoImage 对象,当您从系统中的任何位置打开文件或将文件保存在特定位置或位置时,filedialog 用于显示对话框。创建由按钮组成的 Tkinder 窗口的函数-

Python3

# Create a window
root = Tk()
 
# Set Title as Image Loader
root.title("Image Loader")
 
# Set the resolution of window
root.geometry("550x300 + 300 + 150")
 
# Allow Window to be resizable
root.resizable(width = True, height = True)
 
# Create a button and place it into the window using grid layout
btn = Button(root, text ='open image', command = open_img).grid(
                                        row = 1, columnspan = 4)
root.mainloop()

Button 对象是使用文本“打开图像”创建的。单击它会调用 open_image函数。
将图像放置到窗口上的函数-

Python3

def open_img():
    # Select the Imagename  from a folder
    x = openfilename()
 
    # opens the image
    img = Image.open(x)
     
    # resize the image and apply a high-quality down sampling filter
    img = img.resize((250, 250), Image.ANTIALIAS)
 
    # PhotoImage class is used to add image to widgets, icons etc
    img = ImageTk.PhotoImage(img)
  
    # create a label
    panel = Label(root, image = img)
     
    # set the image as img
    panel.image = img
    panel.grid(row = 2)

openfilename函数将返回图像的文件名。
返回从对话框中选择的文件名的函数-

Python3

def openfilename():
 
    # open file dialog box to select image
    # The dialogue box has a title "Open"
    filename = filedialog.askopenfilename(title ='"pen')
    return filename

要运行此代码,请以扩展名 .py 保存它,然后打开 cmd(命令提示符)并移动到保存文件的位置,然后编写以下内容 -

python "filename".py 

然后按回车,它将运行。或者可以通过简单地双击您的 .py 扩展文件直接运行。输出:
https://media.geeksforgeeks.org/wp-content/uploads/20191121233525/Screencast-from-Thursday-21-November-2019-111137-IST2.webm