微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

“PhotoImage Error”对象没有属性 PhotoImage error 当与类和对象一起使用时

如何解决“PhotoImage Error”对象没有属性 PhotoImage error 当与类和对象一起使用时

我正在使用 OOP 概念在 python 中开发名为 颜色检测 的项目。在这个项目中,用户必须通过从系统中选择图像或从网络摄像头拍照来输入图像。 我几乎完成了项目,但出现运行时错误。 这是我的代码块:

self.root = Tk()
....
....
self.capture_frame = Frame(self.root,width=750,height=550,bg='black')
....
....

def openCamera(self,capture_frame):
    self.hide_all_frames()
    self.capture_frame.pack()
    
    #button
    snapshot_btn = Button(capture_frame,text="CAPTURE",width=13,bg = '#1cbab5',fg = 'black',font = ('dsrom 10',12,'bold'),command = self.snapshot).place(x=170,y=515)
    
    BACK_btn = Button(capture_frame,text="BACK",bg = 'red',command = lambda :self.firstFrame(self.image_selection_frame)).place(x=400,y=515)
    
    #Labels
    self.cam_area = Label(capture_frame,bg = 'black')
    self.cam_area.place(x=50,y=20)  
    
    self.cap = cv.VideoCapture(0)
    
    #while to open camra 
    while True:
        self.img = self.cap.read()[1]
        self.img = cv.flip(self.img,1)
        self.img1 = cv.cvtColor(self.img,cv.COLOR_BGR2RGB)
        self.img = ImageTk.PhotoImage(image = PIL.Image.fromarray(self.img1))
        self.cam_area['image'] = self.img
        self.root.update()

    self.cap.release()
    cv.destroyAllWindows()

当单独运行而不创建任何函数方法时,此代码运行良好。 当我尝试将它包含在我的项目中时它运行良好,甚至它捕获图像并将其保存在系统中。 但是当我想关闭相机并尝试重新打开它时出现错误

[ WARN:0] global /tmp/pip-req-build-j6tkfxml/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
Exception in Tkinter callback

Traceback (most recent call last):
File "/usr/lib/python3.9/tkinter/__init__.py",line 1892,in __call__
return self.func(*args)
File "/home/safehouse/Desktop/ColorDetection.py",line 145,in <lambda>
font = ('dsrom 10',10,command=lambda 
:self.capturecmd(self.capture_frame)).place(x=450,y=130)
File "/home/safehouse/Desktop/ColorDetection.py",line 205,in capturecmd
self.img1 = cv.cvtColor(self.img,cv.COLOR_BGR2RGB)
cv2.error: OpenCV(4.5.1) /tmp/pip-req-build-j6tkfxml/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion Failed) !_src.empty() in function 'cvtColor'

当我捕获然后关闭应用程序而不重新启动相机时,我收到以下错误

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.9/tkinter/__init__.py",in __call__
  return self.func(*args)
File "/home/safehouse/Desktop/ColorDetection.py",in <lambda>
  font = ('dsrom 10',line 206,in capturecmd
   self.img = ImageTk.PhotoImage(master = self.cam_area,image = Image.fromarray(self.img1))
File "/usr/lib/python3/dist-packages/PIL/ImageTk.py",line 112,in 
__init__
   self.__photo = tkinter.PhotoImage(**kw)
File "/usr/lib/python3.9/tkinter/__init__.py",line 4064,in __init__
   Image.__init__(self,'photo',name,cnf,master,**kw)
File "/usr/lib/python3.9/tkinter/__init__.py",line 4009,in __init__
   self.tk.call(('image','create',imgtype,) + options)
_   tkinter.TclError: can't invoke "image" command: application has been destroyed
Exception ignored in: <function PhotoImage.__del__ at 0x7f866f265ca0>
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/PIL/ImageTk.py",line 118,in __del__
   name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

当我使用 root = tk() 我得到 Photoimage() 属性错误但是当我使用

root = Toplevel()

我摆脱了上面的照片图像错误,但后来又出现了新错误

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.9/tkinter/__init__.py",command=lambda :self.capturecmd(self.capture_frame)).place(x=450,line 207,in capturecmd
  self.cam_area['image'] = self.img
File "/usr/lib/python3.9/tkinter/__init__.py",line 1657,in __setitem__
  self.configure({key: value})
File "/usr/lib/python3.9/tkinter/__init__.py",line 1646,in configure
  return self._configure('configure',kw)
File "/usr/lib/python3.9/tkinter/__init__.py",line 1636,in _configure
  self.tk.call(_flatten((self._w,cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!toplevel.!frame3.!label"

我已经尝试过并到处搜索并尝试使用不同版本的软件包,但当时没有真正起作用。 我所有的项目都运行良好,相机也在拍照并保存。但是由于上述错误,我无法在不关闭整个项目的情况下重新启动相机。由于 Photoimage 错误,它删除了相机屏幕。

如果有人能帮忙,那将是一个很大的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。