Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageTk.getimage() is broken (again) #6020

Closed
muditl opened this issue Feb 4, 2022 · 6 comments · Fixed by #6032
Closed

ImageTk.getimage() is broken (again) #6020

muditl opened this issue Feb 4, 2022 · 6 comments · Fixed by #6032
Labels
Bug Any unexpected behavior, until confirmed feature. Windows

Comments

@muditl
Copy link

muditl commented Feb 4, 2022

What did you do?

Tried to convert a PhotoImage to a Image using the ImageTk library function

What did you expect to happen?

Get an Image from the PhotoImage

What actually happened?

got a TclError:
dir..\lib\site-packages\PIL\ImageTk.py", line 279, in getimage
photo.tk.call("PyImagingPhotoGet", photo, block.id)
_tkinter.TclError: invalid command name "PyImagingPhotoGet"

What are your OS, Python and Pillow versions?

  • OS: Windows 10
  • Python: 3.10.2
  • Pillow: 8.0.1
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
p_img = PhotoImage(file='dir/img_name')
image = ImageTk.getimage(p_img)
@radarhere
Copy link
Member

When you say "again", could you elaborate on this? Are you saying that it has been broken with some Pillow versions for you and not others?

Bear with me if you know this and think it is obvious, but

p_img = PhotoImage(file='dir/img_name')
image = ImageTk.getimage(p_img)

could be simplified to

image = Image.open('dir/img_name')

You may have just used that specific code to demonstrate the general problem however.

Also, if you look at https://pillow.readthedocs.io/en/stable/installation.html#python-support, you will see that Pillow 8.0.1 is not intended to support Python 3.10. I doubt it would solve your problem, but I would recommend upgrading to a newer version of Pillow.

@muditl
Copy link
Author

muditl commented Feb 4, 2022

Greetings,
I was referring to a previous issue (from 2019) that has been long solved. In hindsight, maybe referencing such an old issue was not useful.

Yes, I am aware that I can use the code provided, and yes, you are correct that I used the code to demonstrate the issue.
The actual code is to resize a PhotoImage and I don't know any other way of doing so without converting to an Image. I can make it work without, but still thought useful to open the issue. Suggestions to resize without conversion would be very helpful!

Thank you for the update suggestion. I am now running Pillow 9.0.0, however the issue still persists.

@radarhere
Copy link
Member

Hi. Another question just to provide context - what version of Tkinter are you using?

>>> from PIL import _tkinter_finder
>>> _tkinter_finder.tk_version
'8.6'

@muditl
Copy link
Author

muditl commented Feb 4, 2022

It is 8.6

@radarhere
Copy link
Member

Ok, I've realised what the problem is - getimage() does not call _imagingtk.tkinit like PhotoImage.paste does. This means that the Tcl commands are not necessarily created.

I've created PR #6032 to resolve this. If you would like an immediate solution, I find that the following code works.

from tkinter import *
from PIL import Image, ImageTk
root = Tk()

# Clumsy way to activate Pillow's tkinter hooks
ImageTk.PhotoImage("RGB").paste(Image.new("RGB", (1, 1)))

p_img = PhotoImage("hopper.jpg")
image = ImageTk.getimage(p_img)

@muditl
Copy link
Author

muditl commented Feb 8, 2022

Thanks! I'll close this issue now.

@muditl muditl closed this as completed Feb 8, 2022
@radarhere radarhere added the Bug Any unexpected behavior, until confirmed feature. label Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Any unexpected behavior, until confirmed feature. Windows
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants