本文介绍了AttributeError:'PhotoImage'对象没有属性'_PhotoImage__photo'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on Yolo3-4-PY to implement it with tkinter.

我到处都在抬头,但无法解决问题.

I've looked up everywhere but not able to resolve the issue.

运行程序时,显示画布,但是单击开始视频"( btton )时,出现以下错误:

When I run the program the canvas is displayed but when I click on Start Video(btton) I get the following error:

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

Loading weights from weights/yolov3.weights...Done!/usr/local/lib/python3.5/dist-packages/PIL/ImageTk.py:119: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison if mode not in ["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图像.

Issue

In the line imgtk = ImageTk.PhotoImage(image=cv2image), you are passing a numpy array (cv2image) as input to ImageTk.PhotoImage. But the source code of PIL.ImageTk mentions that it requires a PIL image.

这是 PIL的源代码.ImageTk 提到了PhotoImage的 init ().

This is what source code of PIL.ImageTk mentions for init() of PhotoImage.

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图像,然后将其传递给ImageTk.PhotoImage().

Solution

So basically, you will have to convert the numpy array to a PIL Image and then pass it to ImageTk.PhotoImage().

那么,您可以将imgtk = ImageTk.PhotoImage(image=cv2image)行替换为imgtk = ImageTk.PhotoImage(image=PIL.Image.fromarray(cv2image))吗?

So, can you replace the line imgtk = ImageTk.PhotoImage(image=cv2image) with imgtk = ImageTk.PhotoImage(image=PIL.Image.fromarray(cv2image))?

这会将numpy数组转换为PIL图像,并将其传递到方法中.

This would convert the numpy array to a PIL Image and it would be passed into the method.

我从此来源.

这篇关于AttributeError:'PhotoImage'对象没有属性'_PhotoImage__photo'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 18:17