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

Support more options for reading/writing images in Tkinter #118271

Open
serhiy-storchaka opened this issue Apr 25, 2024 · 2 comments
Open

Support more options for reading/writing images in Tkinter #118271

serhiy-storchaka opened this issue Apr 25, 2024 · 2 comments
Labels
3.13 new features, bugs and security fixes topic-tkinter type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Apr 25, 2024

Feature or enhancement

PhotoImage has method write() which writes the image (or its part) to the file. But it lacks methods for two other related subcommand -- read, to read the image from the file, and data, to get the image data. I propose to add methods read() and data().

Also, the write() method can support two new options: background and grayscale.

Linked PRs

@serhiy-storchaka serhiy-storchaka added type-feature A feature request or enhancement topic-tkinter 3.13 new features, bugs and security fixes labels Apr 25, 2024
@serhiy-storchaka
Copy link
Member Author

Note that some Tk commands have options which cannot be directly translated to Python parameters: class, in, from. Usually this is worked around by adding an underscore at the end. But in PhotoImage.write() the -from option was translated to from_coords. There is a precedence of using from_ in other place, so I used from_ in the new copying method (#118225) and in read() added here. It creates inconsistency with the write() method. Maybe we should use from_coords in all PhotoImage methods?

cc @terryjreedy

@serhiy-storchaka
Copy link
Member Author

Yet one note: read -from does not work correctly with GIF and PNG images, unlike to copy -from. This is an old Tk bug https://core.tcl-lang.org/tk/tktview/1576528. A workaround is to read the whole file and then copy a region from the new image by using the new copying method.

We may get reports about broken behavior when people start to use the new method with this option, but I think that it is worth to implement it. The Tk bug can finally be fixed in the future releases, and it works at least with PPM files.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Apr 25, 2024
…kinter

* Add PhotoImage.read() to read an image from a file.
* Add PhotoImage.data() to get the image data.
* Add background and grayscale parameters to PhotoImage.write().
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Apr 25, 2024
…kinter

* Add PhotoImage.read() to read an image from a file.
* Add PhotoImage.data() to get the image data.
* Add background and grayscale parameters to PhotoImage.write().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 new features, bugs and security fixes topic-tkinter type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant