【发布时间】:2023-04-07 17:41:01
【问题描述】:
我正在尝试使用 Pillow 旋转 image:
img = Image.open("./assets/aircraftCarrier/aircraftCarrier0.gif")
img = img.rotate(270)
这会旋转图像,但是当我尝试保存它时,Pillow 似乎无法识别文件类型,即使我在保存时输入了格式类型:
img.save("./tempData/img", "GIF")
它带有file with a blank extension
现在这并不重要,只要 tkinter 可以使用 PhotoImage 识别它,但这似乎也不起作用:
img = PhotoImage(img)
label = Label(root, image=img)
label.pack()
我收到此错误消息:
TypeError: __str__ returned non-string (type Image)
我不确定我做错了什么,或者我是否需要对 Pillow 进行更多处理。
非常感谢您的帮助,
乔什
完整代码:
import tkinter as tk
from tkinter import *
import tkinter
from PIL import Image
root = tk.Tk()
root.title("Test")
img = Image.open("./assets/aircraftCarrier/aircraftCarrier0.gif")
img = img.rotate(270)
img.save("./tempData/img", "GIF")
img = PhotoImage(img)
label = Label(root, image=img)
label.pack()
root.mainloop()
完整的错误信息:
Traceback (most recent call last):
File "C:\Users\Joshlucpoll\Documents\Battleships\test.py", line 19, in <module>
label = Label(root, image=img)
File "C:\Users\Joshlucpoll\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2766, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\Joshlucpoll\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2299, in __init__
(widgetName, self._w) + extra + self._options(cnf))
TypeError: __str__ returned non-string (type Image)
【问题讨论】:
-
您正在尝试旋转 GIF,而不是 png 或 jpg。试过 png 或 jpg 看看是否只有 GIF 会出现错误?
-
@BlackThunder 对 jpg 和 png 都进行了尝试,但结果完全相同
-
这看起来不正确:
img = PhotoImage(img)。 Tkinter 的PhotoImage类不将图像作为其第一个参数。
标签: python python-3.x tkinter python-imaging-library photoimage