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

Resize not allowing filter parameter on 16 bit image #8333

Open
Zaaaane opened this issue Aug 27, 2024 · 5 comments · May be fixed by #8422
Open

Resize not allowing filter parameter on 16 bit image #8333

Zaaaane opened this issue Aug 27, 2024 · 5 comments · May be fixed by #8422

Comments

@Zaaaane
Copy link

Zaaaane commented Aug 27, 2024

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

  • OS: macOS Sonoma 14.6.1 (23G93
  • Python: 3.12.3
  • Pillow: 10.4.0
@radarhere
Copy link
Member

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

ImagingResample(Imaging imIn, int xsize, int ysize, int filter, float box[4]) {
struct filter *filterp;
ResampleFunction ResampleHorizontal;
ResampleFunction ResampleVertical;
if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "1") == 0) {
return (Imaging)ImagingError_ModeError();
}
if (imIn->type == IMAGING_TYPE_SPECIAL) {
return (Imaging)ImagingError_ModeError();

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
Copy link

This was reported downstream as wagtail/Willow#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 wagtail/Willow#154 has mode='I'. In 10.3.0 and later, it is opened as I;16

Looking at the changelog, #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
Copy link
Member

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
Copy link

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
Copy link
Member

I've created #8422 to resolve this.

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

Successfully merging a pull request may close this issue.

3 participants