Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将PIL图像转换为Tkinter图像时出错_Python_Python 3.x_Tkinter_Python Imaging Library_Photoimage - Fatal编程技术网

Python 将PIL图像转换为Tkinter图像时出错

Python 将PIL图像转换为Tkinter图像时出错,python,python-3.x,tkinter,python-imaging-library,photoimage,Python,Python 3.x,Tkinter,Python Imaging Library,Photoimage,我正在尝试旋转一个使用枕头: img=Image.open(“/assets/aircraftCarrier/aircraftCarrier0.gif”) 内模=内模旋转(270) 这会旋转图像,但当我尝试保存图像时,枕头似乎无法识别文件类型,即使我在保存图像时放置了格式类型: img.save(“./tempData/img”、“GIF”) 它提出了一个解决方案 现在,只要tkinter能用PhotoImage识别它,这其实并不重要,但这似乎也不起作用: img=PhotoImage(im

我正在尝试旋转一个使用枕头:

img=Image.open(“/assets/aircraftCarrier/aircraftCarrier0.gif”)
内模=内模旋转(270)
这会旋转图像,但当我尝试保存图像时,枕头似乎无法识别文件类型,即使我在保存图像时放置了格式类型:

img.save(“./tempData/img”、“GIF”)
它提出了一个解决方案

现在,只要tkinter能用PhotoImage识别它,这其实并不重要,但这似乎也不起作用:

img=PhotoImage(img)
标签=标签(根,图像=img)
label.pack()
我收到以下错误消息:

TypeError: __str__ returned non-string (type Image)
我真的不确定我做错了什么,或者我是否需要对枕头做更多的处理

非常感谢您的帮助

乔希


完整代码:

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)
将tkinter作为tk导入
从tkinter进口*
进口tkinter
从PIL导入图像
root=tk.tk()
根标题(“测试”)
img=Image.open(“./assets/aircraftCarrier/aircraftCarrier0.gif”)
内模=内模旋转(270)
img.save(“/tempData/img”、“GIF”)
img=照片图像(img)
标签=标签(根,图像=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)
回溯(最近一次呼叫最后一次):
文件“C:\Users\Joshlucpoll\Documents\Battleships\test.py”,第19行,在
标签=标签(根,图像=img)
文件“C:\Users\Joshlucpoll\AppData\Local\Programs\Python\Python37-32\lib\tkinter\\ uuu init\uu.py”,第2766行,在\uu init中__
小部件。_u初始化(自、主、标签、cnf、kw)
文件“C:\Users\Joshlucpoll\AppData\Local\Programs\Python\Python37-32\lib\tkinter\\ uuuuu init\uuuu.py”,第2299行,在\uu init中__
(widgetName,self._w)+额外+自选项(cnf))
TypeError:\uuuuu str\uuuuuuuuuuuuuuuuuuuuuuuuuuu返回的非字符串(类型图像)
好的,查看
照片图像的下面几行代码:

从PIL导入图像,ImageTk
image=image.open(“lenna.jpg”)
photo=ImageTk.PhotoImage(图像)
它说:

如果您需要使用其他文件格式,可以使用Python映像 库(PIL)包含的类可以让您在超过30分钟的时间内加载图像 格式化,并将其转换为Tkinter兼容的图像对象

因此,从PIL转换到Tkinter时,似乎需要在
PhotoImage
之前添加
ImageTk

例如:

img=ImageTk.PhotoImage(img)


将此添加到我的程序中确实会使旋转后的图像完美地显示出来

您正在尝试旋转GIF,而不是png或jpg。尝试过png或jpg以查看是否仅使用GIF时出错?@BlackThunder尝试了jpg和png,但结果完全相同这看起来不正确:
img=PhotoImage(img)
。Tkinter的
PhotoImage
类不将图像作为其第一个参数。