'Python Tkinter: AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Edit: when I use image = ImageTk.PhotoImage(resize_image) instead of file=resize_image nothing is displayed on my canvas.
See screenshot.
I am creating an image viewer that is split into four frames with each of which displaying an image separately. Currently, I commented-out 3 out of four frames to focus on fixing the exceptions first.
I am trying to display an image inside a canvas, the latter being embedded inside frame_1 on the top-left corner of the screen. Whenever I run my code I get the stacktrace:
Traceback (most recent call last):
File "c:/Users/EM/Desktop/Scripts/gui/slideshow_model/slide_show_class.py", line 67, in <module>
slideshow_model.show_image(img1)
File "c:/Users/EM/Desktop/Scripts/gui/slideshow_model/slide_show_class.py", line 55, in show_image
image = ImageTk.PhotoImage(file=resize_image)
File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 89, in __init__
image = _get_image_from_kw(kw)
File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 58, in _get_image_from_kw
return Image.open(source)
File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2852, in open
prefix = fp.read(16)
AttributeError: 'Image' object has no attribute 'read'
Exception ignored in: <function PhotoImage.__del__ at 0x0000017EAD432D08>
Traceback (most recent call last):
File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 118, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Here is my class:
import tkinter as tk
from PIL import ImageTk, Image
root = tk.Tk()
class SlideshowModel():
def __init__(self, master):
self.master = root
master.title('Basic Image Viewer')
root.iconbitmap('../img/favicon.ico')
root.state('zoomed')
s_w = int(root.winfo_screenwidth())
s_h = int(root.winfo_screenheight())
self.grid_w = s_w // 2
self.grid_h = s_h // 2
self.frame_1 = tk.Frame(master, height=self.grid_h,
width=self.grid_w, bd=0)
self.frame_1.grid(column=0, row=0)
self.can = tk.Canvas(
self.frame_1, width=(self.grid_w - 50), height=(self.grid_h - 50), bg="red")
self.can.grid(row=0, column=0)
self.frame_2 = tk.Frame(master, height=self.grid_h,
width=self.grid_w, bd=0, bg="black")
self.frame_2.grid(column=1, row=0)
self.frame_3 = tk.Frame(master, height=self.grid_h,
width=self.grid_w, bd=0, bg="black")
self.frame_3.grid(column=0, row=1)
self.frame_4 = tk.Frame(master, height=self.grid_h,
width=self.grid_w, bd=0, bg="black")
self.frame_4.grid(column=1, row=1)
# should return the image object
def resize_image(self, img_path):
image = Image.open(img_path)
w_coeff = image.width / self.grid_w
h_coeff = image.height / self.grid_h
w_coeff = 1 / w_coeff if w_coeff > 1 else w_coeff
h_coeff = 1 / h_coeff if h_coeff > 1 else h_coeff
# pick the smallest coeff to get the image as small
# as should be
coeff = min(w_coeff, h_coeff)
image = image.resize(
(int(image.width * coeff), int(image.height * coeff)), Image.ANTIALIAS)
return image
# this function should show returned image
# takes: image object, master frame
def show_image(self, resize_image):
image = ImageTk.PhotoImage(resize_image)
# label = tk.Label(frame_x, image=image, bd=0)
self.can.create_image(0, 0, image=image, anchor='nw')
slideshow_model = SlideshowModel(root)
img1 = slideshow_model.resize_image('../img/sample.jpg')
slideshow_model.show_image(img1)
root.mainloop()
What am I missing?
Update: Now the window opens with no exceptions, but the image is still not displayed, I colored the canvas with color red and reduced its width and height by 50px to be able to differentiate it from the frame, the latter being colored (grey/white).
Solution 1:[1]
The problem is that my image = Image.open(...) was being garbage collected, so I had to add self. so it is not destroyed. See code snippet below:
def resize_image(self, img_path):
self.image = Image.open(img_path)
w_coeff = self.image.width / self.grid_w
h_coeff = self.image.height / self.grid_h
w_coeff = 1 / w_coeff if w_coeff > 1 else w_coeff
h_coeff = 1 / h_coeff if h_coeff > 1 else h_coeff
# pick the smallest coeff to get the image as small
# as should be
coeff = min(w_coeff, h_coeff)
self.image = self.image.resize(
(int(self.image.width * coeff), int(self.image.height * coeff)), Image.ANTIALIAS)
return self.image
def show_image(self, resize_image):
self.image_tk = ImageTk.PhotoImage(resize_image)
self.can.create_image(0, 0, image=self.image_tk, anchor='center')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
