【发布时间】: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