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

Ensure Tkinter hook is activated for getimage() #6032

Merged
merged 2 commits into from Mar 27, 2022

Conversation

radarhere
Copy link
Member

Resolves #6020

When PhotoImage.paste calls "PyImagingPhoto", it catches a TclError in case the tkinter hooks have not been activated yet. If they haven't, then it calls _imagingtk.tkinit, which calls TkImaging_Init, which creates the "PyImaging" Tcl commands.

ImageTk.getimage does not handle the situation where the tkinter hooks have not been activated yet.

Pillow/src/PIL/ImageTk.py

Lines 274 to 281 in ab8125f

def getimage(photo):
"""Copies the contents of a PhotoImage to a PIL image memory."""
im = Image.new("RGBA", (photo.width(), photo.height()))
block = im.im
photo.tk.call("PyImagingPhotoGet", photo, block.id)
return im

So the user from the issue has reported a TclError being raised. This was missed in #3814.

As for testing this, although test_photoimage() looks like it is only testing getimage(), when PhotoImage is called with an image as the first argument, it calls paste immediately. In contrast, the test that I've modified just calls getimage(), and so when the test suite is run in reverse, this will be called first.

@radarhere radarhere added the Bug Any unexpected behavior, until confirmed feature. label Mar 1, 2022
@hugovk hugovk merged commit 57a5066 into python-pillow:main Mar 27, 2022
@radarhere radarhere deleted the tk branch March 27, 2022 21:01
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ImageTk.getimage() is broken (again)
2 participants