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.
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?
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.
Activity
serhiy-storchaka commented on Apr 25, 2024
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 commented on Apr 25, 2024
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.
pythongh-118271: Support more options for reading/writing images in T…
pythongh-118271: Support more options for reading/writing images in T…
gh-118271: Support more options for reading/writing images in Tkinter (…
pythongh-118271: Support more options for reading/writing images in T…