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 for Pillow v10.1 (re: setting image.mode) #1044

Closed
tcompa opened this issue Oct 16, 2023 · 8 comments · Fixed by #1045
Closed

Support for Pillow v10.1 (re: setting image.mode) #1044

tcompa opened this issue Oct 16, 2023 · 8 comments · Fixed by #1045

Comments

@tcompa
Copy link

tcompa commented Oct 16, 2023

Hi there, and thanks for your support.

In https://github.com/imageio/imageio/blob/master/imageio/plugins/pillow.py#L312, the mode attribute of a PIL.Image object is set directly, as in

image.mode = desired_mode

I think this is not working any more as of the recent Pillow v10.1:

If you attempt to set the mode of an image directly, e.g. im.mode = "RGBA", you will now receive an AttributeError. This is not about removing existing functionality, but instead about raising an explicit error to prevent later consequences. The convert method is the correct way to change an image’s mode.
(https://pillow.readthedocs.io/en/stable/releasenotes/10.1.0.html#setting-image-mode)


The minimal failing example would be something like

from imageio.plugins.pillow import Image

img = Image.Image()
img.mode = "RGBA"

This example goes through with Pillow v10.0, but with Pillow v10.1 if fails as in:

Traceback (most recent call last):
  File "/tmp/x.py", line 4, in <module>
    img.mode = "RGBA"
AttributeError: can't set attribute 'mode'

EDIT: the test above is with imageio 2.31.5


(original issue in our repo, for the record: fractal-analytics-platform/fractal-tasks-core#570)

lagru added a commit to lagru/scikit-image that referenced this issue Oct 16, 2023
An update to Pillow (10.1.0) breaks for most recent imageio (<=2.31.5).
See imageio/imageio#1044 [1] for the upstream issue.

[1] imageio/imageio#1044
mkcor pushed a commit to scikit-image/scikit-image that referenced this issue Oct 16, 2023
A Pillow update (10.1.0) breaks for most recent imageio (<=2.31.5).
See imageio/imageio#1044 [1] for the upstream issue.

[1] imageio/imageio#1044
@FirefoxMetzger
Copy link
Contributor

👍 good find. This does break our pillow plugin in a few places, because we have reasons to want those later consequences.

In a nutshell, the convert function from pillow doesn't allow raw modes as input; however, using them allows us to do certain performance optimizations. For example:

elif image.format == "PNG" and image.mode == "I":
# By default, pillows unpacks 16-bit grayscale PNG into 32-bit
# integers due to limited 16-bit support in pillow itself. However,
# recent versions can directly unpack into a 16-bit buffer, which is
# more correct (and more efficient) in our scenario.
if sys.byteorder == "little":
desired_mode = "I;16"
else: # pragma: no cover
# can't test big-endian in GH-Actions
desired_mode = "I;16B"
try:
Image._getdecoder(desired_mode, "raw", "I;16B")
except ValueError:
warnings.warn(
"Loading 16-bit (uint16) PNG as int32 due to limitations "
"in pillow's PNG decoder. This will be fixed in a future "
"version of pillow which will make this warning dissapear.",
UserWarning,
)
else: # pragma: no cover
# Let pillow know that it is okay to return 16-bit
image.mode = desired_mode

I need to see if/how we can keep this functionality given the recent changes.

@FirefoxMetzger
Copy link
Contributor

I tried to "sidestep" the Image.mode problem by setting Image._mode instead until there is a better solution available.

Unfortunately, I found some additional problems related to GIF writing (among other things, one of our test images is now detected as a "compression bomb" by Pillow) and noticed that the JPEG writer is flakey on my local Mac (produces non-deterministic results that cause sporadic test failures).

Long story short, I will pin Pillow to v10.0.0 until I can take a better look at all the changes introduced by pillow v10.1 (PR lives here #1046).

ericpre added a commit to ericpre/rosettasciio that referenced this issue Oct 19, 2023
sitic added a commit to cardiacvision/optimap that referenced this issue Oct 19, 2023
Pillow 10.1.0 introduces incompatabilities with scikit-image.

Revert when
scikit-image/scikit-image#7207
imageio/imageio#1044
is fixed.
@mgorny
Copy link
Contributor

mgorny commented Nov 6, 2023

Ping. Have you been able to make any progress? Given the history of security vulnerabilities in pillow, we're a bit nervous about the pin. FWICS the newest vulnerability is in <10 but this can change at any moment.

@FirefoxMetzger
Copy link
Contributor

A little, but it is still not done. I found a solution for the problems that Pillow v10.1.0 causes in our old Pillow plugin. Now all tests pass locally on my Windows workstation but still break on CI on Linux, so I'm currently looking into those.

@FirefoxMetzger
Copy link
Contributor

@mgorny Done. ImageIO once again works with all recent pillow versions and I've removed the pin. Will be part of Monday's release :)

@mgorny
Copy link
Contributor

mgorny commented Nov 7, 2023

Thank you!

@GaetanLepage
Copy link

@mgorny Done. ImageIO once again works with all recent pillow versions and I've removed the pin. Will be part of Monday's release :)

Thanks so much @FirefoxMetzger ! Do you know if the next release will happen soon ?

@FirefoxMetzger
Copy link
Contributor

FirefoxMetzger commented Nov 14, 2023

Thanks for the ping @GaetanLepage . It should have been released on Monday, but was blocked by failing tests on CD (the same tests passed in CI so this is an interesting failure).

I will have a look and trigger a release after I fixed the problem :)

Edit: the reason for the failure is that pyav has released a new version major over the weekend and this is breaking one of our tests :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants