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

RGBA Image.resize with Nearest Neighbor Interpolation introduces new RGB Values #5300

Closed
gerhardneuhold opened this issue Mar 2, 2021 · 5 comments · Fixed by #5304
Closed

Comments

@gerhardneuhold
Copy link

gerhardneuhold commented Mar 2, 2021

What did you do?

Resizing an RGBA image with Image.resize and nearest neighbor interpolation.

What did you expect to happen?

Image resized to given size, no new RGB values introduced.

What actually happened?

New RGB values introduced.

What are your OS, Python and Pillow versions?

  • OS: Linux
  • Python: 3.8.6
  • Pillow: 6.2.1

Python Example

import numpy as np
from PIL import Image

image = np.asarray([[[1, 1, 0, 128]], [[2, 2, 0, 64]]], dtype=np.uint8)

print("Input")
print(image)
print("")

image = Image.fromarray(image)
image = image.resize((1, 1), Image.NEAREST)
image = np.asarray(image)

print("Output")
print(image)
Input
[[[  1   1   0 128]]
 [[  2   2   0  64]]]

Output
[[[ 3  3  0 64]]]
@radarhere
Copy link
Member

radarhere commented Mar 2, 2021

This may not be related, but Pillow 6.2.1 is a bit old. Is there any reason you haven't upgraded to a newer Pillow version?

@gerhardneuhold
Copy link
Author

gerhardneuhold commented Mar 2, 2021

The version is fixed for the project I am working on but it's also happening with Pillow 8.1.0. Maybe issue #4516 is related?

@radarhere
Copy link
Member

You'll likely think this is a basic question, but could you link me to something explaining that nearest neighbor interpolation shouldn't introduce new values?

@nulano
Copy link
Contributor

nulano commented Mar 3, 2021

You'll likely think this is a basic question, but could you link me to something explaining that nearest neighbor interpolation shouldn't introduce new values?

From https://pillow.readthedocs.io/en/stable/handbook/concepts.html#PIL.Image.NEAREST:

Pick one nearest pixel from the input image. Ignore all other input pixels.

I would interpret that as meaning that each pixel in the output will be equal to one of the original pixels, i.e. not introducing new values.

@nulano
Copy link
Contributor

nulano commented Mar 3, 2021

I've created #5304 to disable premultiplying with NEAREST resampling.

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