python - 属性错误 : 'PhotoImage' object has no attribute '_PhotoImage__photo'

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

我正在研究 Yolo3-4-PY 以使用 tkinter 实现它。

我到处查找,但无法解决问题。

当我运行程序时,会显示 Canvas ,但是当我单击“开始视频”( btton ) 时,出现以下错误:

从 weights/yolov3.weights 加载权重...完成!
/usr/local/lib/python3.5/dist-packages/PIL/ImageTk.py:119:FutureWarning:元素比较失败;改为返回标量,但将来会执行元素比较
如果模式不在 ["1", "L", "RGB", "RGBA"] 中:

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1553, in __call__
return self.func(*args)
File "webcam_demo.py", line 13, in start_video
show_frame()
File "webcam_demo.py", line 39, in show_frame
imgtk = ImageTk.PhotoImage(image=cv2image)
File "/usr/local/lib/python3.5/dist-packages/PIL/ImageTk.py", line 120, in 
__init__
mode = Image.getmodebase(mode)
File "/usr/local/lib/python3.5/dist-packages/PIL/Image.py", line 313, in 
getmodebase
return ImageMode.getmode(mode).basemode
File "/usr/local/lib/python3.5/dist-packages/PIL/ImageMode.py", line 55, in 
getmode
return _modes[mode]
TypeError: unhashable type: 'numpy.ndarray'
Exception ignored in: <bound method PhotoImage.__del__ of 
<PIL.ImageTk.PhotoImage object at 0x7f4b73f455c0>>
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/PIL/ImageTk.py", line 130, in 
__del__    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

最佳答案

问题
imgtk = ImageTk.PhotoImage(image=cv2image) 行中,您将一个 numpy 数组 (cv2image) 作为输入传递给 ImageTk.PhotoImage。但是 PIL.ImageTk 的源代码提到它需要一个 PIL 图像。
这是PIL.ImageTk的源代码中提到的PhotoImage的 init ()。

class PhotoImage(object):
    .....
    :param image: Either a PIL image, or a mode string.  If a mode string is
              used, a size must also be given.
解决方案
所以基本上,您必须将 numpy 数组转换为 PIL Image,然后将其传递给 ImageTk.PhotoImage()。
那么,你能用 imgtk = ImageTk.PhotoImage(image=cv2image) 替换 imgtk = ImageTk.PhotoImage(image=PIL.Image.fromarray(cv2image)) 行吗?
这会将 numpy 数组转换为 PIL Image 并将其传递到方法中。
引用
我从 this source 中提取了将 numpy 数组转换为 PIL Image 的代码。

关于python - 属性错误 : 'PhotoImage' object has no attribute '_PhotoImage__photo' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50662773/

相关文章:

python - 澄清 word2vec `generate_batch()` 是如何工作的?

python - 如何防止anaconda环境读取本地安装的库

python - 在 Tkinter 中将图像添加到按钮

python - 从 Skimage 高程图生成 3D 表面图(2D numpy.ndarray)

python - 为什么后来声明的小部件首先出现?

python - 有没有办法将 Python/Tkinter 连接到已经运行的 Tcl/Tk 应用程序?

python - types.MethodType 与 functools.partial

python - 添加 "+"和 "-"的最佳方法?

python - Unicode 字符无法在终端 python 中正确打印

python - 如何在 "subprocess.run"函数中使用多个变量?