I am running into an issue with self.photo = ImageTk.PhotoImage(self.resized_img). It tells me that AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'. Am I doing this incorrectly with the thumbnail function?

 def fileDialog(self):
    self.filename = filedialog.askopenfilename(title="Select file")
    self.label = ttk.Label(self.labelFrame, text = "")
    self.label.grid(column = 1, row = 2)
    self.label.configure(text=os.path.basename(self.filename))

    self.img = Image.open(self.filename)

    self.thumbNail_img = self.img.thumbnail((512, 512))

    self.photo = ImageTk.PhotoImage(self.thumbNail_img)
    self.display = ttk.Label(image=self.photo)
    self.display.place(relx=0.10, rely=0.10)

Error:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
 File "gui.py", line 44, in fileDialog
self.photo = ImageTk.PhotoImage(self.resized_img)
  File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\ImageTk.py", line 113, in __init__
mode = Image.getmodebase(mode)
  File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\Image.py", line 326, in getmodebase
return ImageMode.getmode(mode).basemode
  File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\ImageMode.py", line 56, in getmode
return _modes[mode]

1 Answers

1
furas On Best Solutions

Try

print(type(self.img), type(self.thumbNail_img)) 

and you see

<class 'PIL.JpegImagePlugin.JpegImageFile'> <class 'NoneType'>

It means than self.img is Image but self.thumbNail_img is None

thumbnail() doesn't create new image. It changes original image in self.img and returns None.
It works "in-place". Doc: Image.thumbnail

So you have to use self.img to display it

 self.img = Image.open(self.filename)

 self.img.thumbnail((512, 512))

 self.photo = ImageTk.PhotoImage(self.img)

If you need original image then you can .copy() it

 self.img = Image.open(self.filename)

 self.thumbNail_img = self.img.copy()

 self.thumbNail_img.thumbnail((512, 512))

 self.photo = ImageTk.PhotoImage(self.thumbNail_img)