【问题标题】:PIL has ImageTk KeyError <PIL.ImageTk.PhotoImage object at 0x0000025A48201690>PIL 有 ImageTk KeyError <PIL.ImageTk.PhotoImage object at 0x0000025A48201690>
【发布时间】:2022-01-21 20:02:46
【问题描述】:

我正在制作一个照片显示应用程序,以替换 Windows 的默认应用程序。我正在使用 Pillow 和 ImageTk 来显示它们,但是我的问题是我正在尝试使用一些代码将图像旋转到不同的方向。当我运行代码时,我遇到了这个问题:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\james\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "c:\Users\james\OneDrive\Documents\Coding\Python\Apps for Computer\photo_viewer.py", line 51, in rotate_left
    newimg = ImageTk.PhotoImage(img, size=(img.width(), img.height()))
  File "C:\Users\james\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\ImageTk.py", line 108, in __init__
    mode = Image.getmodebase(mode)
  File "C:\Users\james\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\Image.py", line 267, in getmodebase
    return ImageMode.getmode(mode).basemode
  File "C:\Users\james\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\ImageMode.py", line 74, in getmode
    return _modes[mode]
KeyError: <PIL.ImageTk.PhotoImage object at 0x00000277E66616C0>

所以,这里是代码的改进版本,向您展示它是如何工作的:

import tkinter
from tkinter.filedialog import askopenfile
from PIL import Image, ImageTk

img_exten = r"*.png  *.bmp  *jpeg  *.bmp  *.ico  *.gif  *.jpg"
filetypes = (
    ("Image Files", img_exten),
    ("All Files", "*.*")
)

selected_image = ""
img = ""

root = tkinter.Tk()


def open_image():
    global selected_image
    global img
    try:
        selected_image = askopenfile(title="Open Image", filetypes=filetypes).name
        root.title(selected_image + " - Photos")
        img_temp = Image.open(selected_image)
        ar = img_temp.width / img_temp.height
        height = 540
        width = int(height * ar)
        root.geometry(str(width + 30) + "x" + str(height + 50))
        img_temp = img_temp.resize((width, height), Image.ANTIALIAS)
        img = ImageTk.PhotoImage(img_temp)
        image_area.create_image(1, 1, image=img, anchor="nw")
    except Exception as e:
        print(e)


def rotate_left():
    global img
    newimg = ImageTk.PhotoImage(img, size=(img.width(), img.height()))
    for x in range(img.width()):
        for y in range(img.height()):
            rgb = '#%02x%02x%02x' % img.get(x, y)
            newimg.put(rgb, (x, y))
    newimg.put(rgb (img.height() - y, x))
    img = newimg
    image_area.create_image(1, 1, image=img, anchor="nw")


image_area = tkinter.Canvas(root, width=960, height=540)
image_area.grid(column=1, row=1)


menu_bar = tkinter.Menu(root)
filemenu = tkinter.Menu(menu_bar, tearoff=0)
filemenu.add_command(label="Open", command=open_image)
filemenu.add_command(label="Close", command=close_image)
menu_bar.add_cascade(label="File", menu=filemenu)

toolsmenu = tkinter.Menu(menu_bar, tearoff=0)
toolsmenu.add_command(label="Rotate left", command=rotate_left)
menu_bar.add_cascade(label="Tools", menu=toolsmenu)

root.config(menu=menu_bar)
tkinter.mainloop()
  • 詹姆斯

【问题讨论】:

  • img 的初始值是一个空字符串 - 作为ImageTk.PhotoImage() 的第一个参数或使用它的任何其他地方都无效。将其初始化为实际图像(可能是 1 x 1 像素)可能是最简单的解决方法。
  • 我认为问题在于,在 open_image 内部,您创建了 img 一个 PhotoImage 实例,然后您再次使用该实例创建另一个 PhotoImage 实例。而是保持原样和global img_temp 然后newimg = ImageTk.PhotoImage(temp_img)。另外,您是否只是想将图像向左旋转?
  • @jasonharper 在代码测试中,它得到了一个图像大声笑
  • @DelriusEuphoria 好的 :D 和右边也是
  • 请记住,旋转图像时需要展开画布。

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


【解决方案1】:

您的代码在任何地方都没有img_temp.rotate

在您的 rotate_left 函数中尝试复制 img_temp 然后旋转它。 这段代码 sn-p 对我有用。

def rotate_left():
    global img
    # pass original image open reference
    global img_temp 
    # make a copy
    extra = img_temp.copy()
    # rotate it
    new = extra.rotate(45, expand = False)
    # self.new.save(new_picture_name)
    # make image
    newimg = ImageTk.PhotoImage(new, size=(img.width(), img.height()))
    # update old image
    img = newimg
    image_area.create_image(1, 1, image=img, anchor="nw")
    # update old image open reference
    img_temp = new

【讨论】:

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