【发布时间】:2022-02-17 23:16:11
【问题描述】:
我正在尝试使用 PIL 版本 6.2.1 在 python 3.7.3 中使用以下代码:
render = ImageTk.PhotoImage(Image.open(pic))
但它会导致如下错误消息:
Traceback (most recent call last):
File "F:/python/test/test10.py", line 12, in <module>
render = ImageTk.PhotoImage(Image.open(pic))
File "C:\Users\erica\AppData\Roaming\Python\Python37\site-packages\PIL\ImageTk.py", line 118, in __init__
self.__photo = tkinter.PhotoImage(**kw)
File "C:\Users\erica\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 3545, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\erica\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 3489, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
Exception ignored in: <function PhotoImage.__del__ at 0x0000027A91FEB0D0>
Traceback (most recent call last):
File "C:\Users\erica\AppData\Roaming\Python\Python37\site-packages\PIL\ImageTk.py", line 124, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
我尝试了不同的 Pillow 版本,尝试按照其他帖子的建议输入类实例,尝试使用 os.chdir(pic_dir)。但它们都不起作用。
【问题讨论】:
-
不知道为什么你正在做的事情不起作用。试试
render = ImageTk.PhotoImage(image=Image.open(pic))和render = ImageTk.PhotoImage(file=pic)(假设pic是一个文件名)。 -
谢谢。回答,但我已经尝试了这两个结果相同的错误
-
当一切都失败时,尝试重新安装 PIL/Pillow。
-
已经尝试重新安装,也可以使用不同的版本。但同样的错误
标签: python python-imaging-library