【问题标题】:PIL ImageTk AttributeError, can't display ImageTk on windowPIL ImageTk AttributeError,无法在窗口上显示 ImageTk
【发布时间】:2020-05-23 07:35:25
【问题描述】:

我一直在开发一个将 PNG、JPG 和 JPEG 文件转换为 .ICO 文件的小程序。这相对简单,但是当我尝试使用 PIL 的 ImageTk 在 Tkinter 中显示选定的 PNG 图像时,我得到了一个奇怪的错误。

from tkinter import *
from tkinter import filedialog
import re
from PIL import Image, ImageTk

root = Tk()
pathToImage = ''
selectedImage = ''
def make_square(im, min_size=256, fill_color = (0, 0, 0)):    # Puts the selected image into a black square
    x, y = im.size
    size = max(min_size, x, y)
    new_im = Image.new('RGB', (size,size), fill_color)
    new_im.paste(im, (int((size - x) / 2), int((size - y) / 2)))
    return new_im

def select_image(): # Function that is run when Select PNG button is clicked
    global pathToImage
    pathToImage = filedialog.askopenfilename(filetypes=[('PNG Files','*.png'),('JPG Files','*.jpg'),('JPEG Files','*.jpeg')]) # Gets path to PNG, JPG or JPEG image
    image = Image.open(pathToImage) # Opens image in PIL
    image = make_square(im=image) # Turns image into square for ICO conversion
    #!!!!!!!!!!!!!!!!!!!!! ERROR Among these 3 lines
    global selectedImage # Here I try to tell Python I'm referring to the global variable selectedImage
    selectedImage = (ImageTk.PhotoImage(image=pathToImage)) # selectedImage is given the value of ImageTk.PhotoImage with the source image being the path of the selected image
    Label(root, image=selectedImage).pack() # Throws an error for some reason
    # Rest of the code works fine
    image.save('output.ico')
    Label(root,text='Converted file stored in the same folder as \'PNG to ICO.py\'').pack()


Button(root,text='Select PNG', command=select_image).pack()

root.mainloop()

我尝试将要显示的图像保存到变量中,但这似乎也不起作用。谁能帮忙指出我做错了什么?我真的很感激。

【问题讨论】:

  • 什么是错误?它告诉我KeyError: the path

标签: python python-3.x image tkinter python-imaging-library


【解决方案1】:

您的代码存在几个问题。

  • 在你的(ImageTk.PhotoImage(image=pathToImage)) 行中你正在传递一个不应该采用的路径(str),ImageTk.PhotoImage 采用Image(path) 的实例。所以把它改成make_square函数返回的图片。

  • 每次点击按钮时,它都会创建一个新的label,如果这是你想要的,那么忽略它,如果不是,那么在你之后在函数select_image之外创建你的标签创建 Button 并稍后在函数中更新它们。

  • 我真的不明白你为什么要使用global,因为你可以在不创建变量pathToImageselectedImage 的情况下实现你的目的,除非你想稍后在程序中访问该图像。


这是您的代码的改进版本。

from tkinter import *
from tkinter import filedialog
import re
from PIL import Image, ImageTk


def make_square(im, min_size=256, fill_color = (0, 0, 0)):    # Puts the selected image into a black square
    x, y = im.size
    size = max(min_size, x, y)
    new_im = Image.new('RGB', (size,size), fill_color)
    new_im.paste(im, (int((size - x) / 2), int((size - y) / 2)))
    return new_im

def select_image(): # Function that is run when Select PNG button is clicked
    pathToImage = filedialog.askopenfilename(filetypes=[('PNG Files','*.png'),('JPG Files','*.jpg'),('JPEG Files','*.jpeg')])
    image = Image.open(str(pathToImage)) # Opens image in PIL
    image = make_square(im=image) # Turns image into square for ICO conversion
    selectedImage = ImageTk.PhotoImage(image=image)
    imglabel.img = selectedImage  # create a reference of the image
    imglabel['image'] = selectedImage
    # selectedImage is given the value of ImageTk.PhotoImage with the source image being the path of the selected image
    # Rest of the code works fine
    image.save('output.ico', 'ICO')
    infolabel['text'] = 'Converted file stored in the same folder as \'PNG to ICO.py\''

root = Tk()

but1 = Button(root,text='Select PNG', command=select_image)
but1.pack()

imglabel = Label(root)
imglabel.pack()

infolabel = Label(root)
infolabel.pack()

root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    相关资源
    最近更新 更多