python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.24k stars 2.23k forks source link

Resize not allowing filter parameter on 16 bit image #8333

Closed Zaaaane closed 1 month ago

Zaaaane commented 2 months ago

When attempting to resize a 16 bit image as shown below, a value error appears and crashes the program: "ValueError: image has wrong mode" The image type is "I;16"

The primary problem is that this worked without error in early July and hasn't been touched in a month. When I opened the code today it gave the error even when reverting to a previously stable version. Below is the relevant code inside a custom cropping method. The coord_tup variable is just a tuple that is working as expected

 new_im = im.crop(coord_tup)
 newSize = (640, 480)
 new_im = new_im.resize(newSize, resample=Image.Resampling.LANCZOS)
 npImg = np.asarray(new_im)
 return npImg

As stated this code used to work fine but when opened this week I get a value error on the line with the resize method, though it will technically work if I change the following

 new_im = new_im.resize(newSize, resample=Image.Resampling.LANCZOS)

->

 new_im = new_im.resize(newSize)

I understand through the API documentation that this should default to NEAREST but I could not find anything stating that LANCZOS would not work with 16 bit imaging

radarhere commented 2 months ago

Just to provide a self-contained code example, I agree that

from PIL import Image
im = Image.new("I;16", (1, 1))
new_im = im.resize((640, 480), resample=Image.Resampling.LANCZOS)

is hitting https://github.com/python-pillow/Pillow/blob/56e3147403d1785141b25f1d822a93e86c1deb98/src/libImaging/Resample.c#L567-L577

The primary problem is that this worked without error in early July

Considering our last release was on July 1, this doesn't make much sense to me. I tested with 10.3.0, the release before that, and the same error occurs.

Stormheg commented 1 month ago

This was reported downstream as https://github.com/wagtail/Willow/issues/154

I tested a few Pillow versions and can report this started happening with Pillow 10.3.0. Pillow 10.2.0 is not affected and resizes without error.

Digging further, it appears that in Pillow 10.2.0 the sample image from https://github.com/wagtail/Willow/issues/154 has mode='I'. In 10.3.0 and later, it is opened as I;16

Looking at the changelog, https://github.com/python-pillow/Pillow/pull/7849 appears to be responsible for that change.

Is this considered a bug / regression? Or should we as end-users do something as a workaround? (use a different resample filter for example)

radarhere commented 1 month ago

This is still an open issue, so it is something to be worked on.

The original post here doesn't mention PNGs, so you might be talking about a slightly different scenario. If your problem is #7849, then the simplest workaround would be to convert the image to I mode after opening - im = im.convert("I").

Stormheg commented 1 month ago

Thanks for the response! I realize the original post doesn't mention PNGs, but assuming the original posters' code used to work before I'd say it is likely a similar scenario.

Given I;16 mode support is limited according to the documentation, would you suggest always converting to I mode before performing image manipulation operations or is support going to be improved in the near future? Trying to gauge what the Willow maintainers should do here - perform the conversion workaround or wait for a new Pillow release.

radarhere commented 1 month ago

I've created #8422 to resolve this.

Stormheg commented 6 days ago

Thanks for fixing @radarhere! I see this has shipped with 11.0.0 and it works perfectly. Are there any plans to backport to the 10.x release series or that now no longer supported?

hugovk commented 6 days ago

Thanks for fixing @radarhere! I see this has shipped with 11.0.0 and it works perfectly.

Good to hear!

Are there any plans to backport to the 10.x release series or that now no longer supported?

No, we don't backport anything: only the latest released version is supported.

Stormheg commented 6 days ago

No, we don't backport anything: only the latest released version is supported.

Alright, thanks. Is this policy documented anywhere? I could not find any clear reference in the documentation.

hugovk commented 6 days ago

We have this at https://pillow.readthedocs.io/en/stable/releasenotes/index.html:

Please use the latest version of Pillow. Functionality and security fixes should not be expected to be backported to earlier versions.

Stormheg commented 6 days ago

Thanks, not sure how I missed that