AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

后端 未结 3 1185
闹比i
闹比i 2021-01-13 15:12

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

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

When I run the program the canvas is d

3条回答
  •  无人共我
    2021-01-13 16:08

    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.

    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.
    

    Solution

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

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

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

    References

    I extracted the code for converting a numpy array to PIL Image from this source.

提交回复
热议问题