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

后端 未结 3 1182
闹比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 15:47

    when you place the image variable in the label , you must initiate the image variable to "image".

    Eg: (CORRECT APPROACH)

    photo = PhotoImage(file = "C://Users//Carl//Downloads//download.png")
    label1 = Label(image = photo)
    label1.pack()
    

    Eg : (WRONG APPROACH)

    photo = PhotoImage(file = "C://Users//Carl//Downloads//download.png")
    label1 = Label(photo)
    label1.pack()
    
    0 讨论(0)
  • 2021-01-13 15:50

    in my case , correct with just simply add this line

    root = tkinter.Tk()
    

    complete code :

    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()
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题