AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
我正在研究Yolo3-4-PY,以使用tkinter来实现它。
我到处都在抬头,但无法解决问题。
当我运行程序时,显示画布,但是当我单击"开始视频"(btton)时,出现以下错误:
从砝码/yolov3.weights中加载砝码...完成!
/usr/local/lib/python3.5/dist-packages/PIL/ImageTk.py:119:FutureWarning:逐元素比较失败;而是返回标量,但将来将执行元素比较
如果模式不在[" 1 "," L "," RGB "," RGBA "]中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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' |
问题
在行
这是PIL.ImageTk的源代码提到的PhotoImage的init()。
1 2 3 4 | 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()。
那么,您可以将
这会将numpy数组转换为PIL图像,并将其传递给方法。
参考
我从该源提取了将numpy数组转换为PIL图像的代码。
在我的情况下,
只需添加此行即可纠正
1 | root = tkinter.Tk() |
完整代码:
1 2 3 4 5 | root = tkinter.Tk() image = PIL.Image.open(r"C:\\Users\\Hamid\\Desktop\\asdasd\\2.jpeg") img = ImageTk.PhotoImage(image) l = Label(image=img) l.pack() |
将image变量放置在标签中时,必须将image变量初始化为" image "。
例如:(正确的方法)
1 2 3 | photo = PhotoImage(file ="C://Users//Carl//Downloads//download.png") label1 = Label(image = photo) label1.pack() |
例如:(错误的方法)
1 2 3 | photo = PhotoImage(file ="C://Users//Carl//Downloads//download.png") label1 = Label(photo) label1.pack() |