python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
11.8k stars 2.13k forks source link

'PhotoImage' object has no attribute '_PhotoImage__photo' depending on img extension and argument/object #8018

Closed chepo92 closed 1 month ago

chepo92 commented 1 month ago

What did you do?

Using tk inter and showing some png and jpeg, depending on how the jpg is imported the library shows some exceptions, eg: if you pass a file path ImageTk.PhotoImage(file = file_path) works ok, but passing a PIL image ImageTk.PhotoImage(Image.open(os.path.join(current_path, "images", "airport.jpg"))) throws an exception

What did you expect to happen?

no warnings, no exceptions, not depending on how to pass the image to function or the extension of the image

What actually happened?

Outputs some exceptions when using jpeg images:

Exception ignored in: <function PhotoImage.__del__ at 0x000002A5854C4A60>
Traceback (most recent call last):
Exception ignored in: <function PhotoImage.__del__ at 0x000002A5854C4A60>
Traceback (most recent call last):
Exception ignored in: <function PhotoImage.__del__ at 0x000002A5854C4A60>
Exception ignored in: <function PhotoImage.__del__ at 0x000002A5854C4A60>
Traceback (most recent call last):
Traceback (most recent call last):
  File "D:\Git\kivy-example\.venv\lib\site-packages\PIL\ImageTk.py", line 132, in __del__
  File "D:\Git\kivy-example\.venv\lib\site-packages\PIL\ImageTk.py", line 132, in __del__
  File "D:\Git\kivy-example\.venv\lib\site-packages\PIL\ImageTk.py", line 132, in __del__
Exception ignored in: <function PhotoImage.__del__ at 0x000002A5854C4A60>
Traceback (most recent call last):
  File "D:\Git\kivy-example\.venv\lib\site-packages\PIL\ImageTk.py", line 132, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
  File "D:\Git\kivy-example\.venv\lib\site-packages\PIL\ImageTk.py", line 132, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Exception ignored in: <function PhotoImage.__del__ at 0x000002A5854C4A60>
Exception ignored in: <function PhotoImage.__del__ at 0x000002A5854C4A60>
Traceback (most recent call last):
  File "D:\Git\kivy-example\.venv\lib\site-packages\PIL\ImageTk.py", line 132, in __del__
Exception ignored in: <function PhotoImage.__del__ at 0x000002A5854C4A60>
Traceback (most recent call last):
  File "D:\Git\kivy-example\.venv\lib\site-packages\PIL\ImageTk.py", line 132, in __del__
Exception ignored in: <function PhotoImage.__del__ at 0x000002A5854C4A60>
Traceback (most recent call last):
Exception ignored in: <function PhotoImage.__del__ at 0x000002A5854C4A60>
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Traceback (most recent call last):
Traceback (most recent call last):
  File "D:\Git\kivy-example\.venv\lib\site-packages\PIL\ImageTk.py", line 132, in __del__
    name = self.__photo.name
  File "D:\Git\kivy-example\.venv\lib\site-packages\PIL\ImageTk.py", line 132, in __del__
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
    name = self.__photo.name
  File "D:\Git\kivy-example\.venv\lib\site-packages\PIL\ImageTk.py", line 132, in __del__
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
    name = self.__photo.name

What are your OS, Python and Pillow versions?

Pillow 9.4.0
Python 3.11.9 (tags/v3.11.9:de54cf5, Apr  2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]
--------------------------------------------------------------------
Python modules loaded from C:\Users\Axel\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\PIL
Binary modules loaded from C:\Users\Axel\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 9.4.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.12.1
--- LITTLECMS2 support ok, loaded 2.14
--- WEBP support ok, loaded 1.2.4
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for libjpeg-turbo 2.1.4
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.0
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.13
--- LIBTIFF support ok, loaded 4.5.0
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------
BLP
Extensions: .blp
Features: open, save, encode
--------------------------------------------------------------------
BMP image/bmp
Extensions: .bmp
Features: open, save
--------------------------------------------------------------------
BUFR
Extensions: .bufr
Features: open, save
--------------------------------------------------------------------
CUR
Extensions: .cur
Features: open
--------------------------------------------------------------------
DCX
Extensions: .dcx
Features: open
--------------------------------------------------------------------
DDS
Extensions: .dds
Features: open, save
--------------------------------------------------------------------
DIB image/bmp
Extensions: .dib
Features: open, save
--------------------------------------------------------------------
EPS application/postscript
Extensions: .eps, .ps
Features: open, save
--------------------------------------------------------------------
FITS
Extensions: .fit, .fits
Features: open, save
--------------------------------------------------------------------
FLI
Extensions: .flc, .fli
Features: open
--------------------------------------------------------------------
FTEX
Extensions: .ftc, .ftu
Features: open
--------------------------------------------------------------------
GBR
Extensions: .gbr
Features: open
--------------------------------------------------------------------
GIF image/gif
Extensions: .gif
Features: open, save, save_all
--------------------------------------------------------------------
GRIB
Extensions: .grib
Features: open, save
--------------------------------------------------------------------
HDF5
Extensions: .h5, .hdf
Features: open, save
--------------------------------------------------------------------
ICNS image/icns
Extensions: .icns
Features: open, save
--------------------------------------------------------------------
ICO image/x-icon
Extensions: .ico
Features: open, save
--------------------------------------------------------------------
IM
Extensions: .im
Features: open, save
--------------------------------------------------------------------
IMT
Features: open
--------------------------------------------------------------------
IPTC
Extensions: .iim
Features: open
--------------------------------------------------------------------
JPEG image/jpeg
Extensions: .jfif, .jpe, .jpeg, .jpg
Features: open, save
--------------------------------------------------------------------
JPEG2000 image/jp2
Extensions: .j2c, .j2k, .jp2, .jpc, .jpf, .jpx
Features: open, save
--------------------------------------------------------------------
MCIDAS
Features: open
--------------------------------------------------------------------
MPEG video/mpeg
Extensions: .mpeg, .mpg
Features: open
--------------------------------------------------------------------
MSP
Extensions: .msp
Features: open, save, decode
--------------------------------------------------------------------
PCD
Extensions: .pcd
Features: open
--------------------------------------------------------------------
PCX image/x-pcx
Extensions: .pcx
Features: open, save
--------------------------------------------------------------------
PIXAR
Extensions: .pxr
Features: open
--------------------------------------------------------------------
PNG image/png
Extensions: .apng, .png
Features: open, save, save_all
--------------------------------------------------------------------
PPM image/x-portable-anymap
Extensions: .pbm, .pgm, .pnm, .ppm
Features: open, save
--------------------------------------------------------------------
PSD image/vnd.adobe.photoshop
Extensions: .psd
Features: open
--------------------------------------------------------------------
SGI image/sgi
Extensions: .bw, .rgb, .rgba, .sgi
Features: open, save
--------------------------------------------------------------------
SPIDER
Features: open, save
--------------------------------------------------------------------
SUN
Extensions: .ras
Features: open
--------------------------------------------------------------------
TGA image/x-tga
Extensions: .icb, .tga, .vda, .vst
Features: open, save
--------------------------------------------------------------------
TIFF image/tiff
Extensions: .tif, .tiff
Features: open, save, save_all
--------------------------------------------------------------------
WEBP image/webp
Features: open, save
--------------------------------------------------------------------
XBM image/xbm
Extensions: .xbm
Features: open, save
--------------------------------------------------------------------
XPM image/xpm
Extensions: .xpm
Features: open
--------------------------------------------------------------------
XVTHUMB
Features: open
import os
import tkinter
import tkintermapview
from PIL import Image, ImageTk

# create tkinter window
root_tk = tkinter.Tk()
root_tk.geometry(f"{1000}x{700}")
root_tk.title("map_view_simple_example.py")

# create map widget
map_widget = tkintermapview.TkinterMapView(root_tk, width=1000, height=700, corner_radius=0)
map_widget.pack(fill="both", expand=True)

# load images
current_path = os.path.join(os.path.dirname(os.path.abspath(__file__)))
plane_image = ImageTk.PhotoImage(Image.open(os.path.join(current_path, "images", "plane.png")).resize((40, 40)))
plane_circle_1_image = ImageTk.PhotoImage(Image.open(os.path.join(current_path, "images", "plane_circle_1.png")).resize((35, 35)))
# plane_circle_1_image = ImageTk.PhotoImage(file = os.path.join(current_path, "images", "plane_circle_1.png"))
plane_circle_2_image = ImageTk.PhotoImage(Image.open(os.path.join(current_path, "images", "plane_circle_2.png")).resize((35, 35)))

# **** Toggle comment this three lines to reproduce *****
# file_path = os.path.join(current_path, "images", "airport.jpg")
# airport_image = ImageTk.PhotoImage(file = file_path)
airport_image = ImageTk.PhotoImage(Image.open(os.path.join(current_path, "images", "airport.jpg")).resize((100, 70)))

blue_marker = ImageTk.PhotoImage(Image.open(os.path.join(current_path, "images", "blue_marker.png")).resize((50, 50)))

def marker_callback(marker):
    print(marker.text)
    marker.delete()

# create markers
marker_1 = map_widget.set_marker(52.476062, 13.394172, text="Plane 1", icon=blue_marker,  command=marker_callback)  # 
marker_2 = map_widget.set_marker(52.352659, 13.499669, text="Plane 2", 
                                  image=airport_image, icon=plane_circle_1_image, command=marker_callback) # 
root_tk.after(3000, lambda: marker_2.change_icon(plane_image))

# set initial position of map widget
map_widget.set_address("Airport Berlin BER")
map_widget.set_zoom(11)

root_tk.mainloop()

# `pyinstaller map_view_marker_icon_images.py --add-data="images/:images/" -y`
radarhere commented 1 month ago

I'm struggling to consistently reproduce your error. Could you try the following simplified version of your code and let us know if there is still a problem?

import os
import tkinter
from PIL import Image, ImageTk

root_tk = tkinter.Tk()
ImageTk.PhotoImage(Image.open("images/airport.jpg"))

Could you also, just for completeness, provide us with a copy of airport.jpg?

chepo92 commented 1 month ago

Couldn't reproduce with provided code, but that opened my mind and i think I discovered the issue is related with the TkinterMapView library instead, as sometimes is throwing randomly a 403 error for the retrieved tiles, I guess it uses PIL internally to render, then this library complains as there is no image to render, closing this, thanks!