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

Method ImageOps.pad doesn't copy palette of original image #6595

Closed
PososikTeam opened this issue Sep 18, 2022 · 1 comment · Fixed by #6596
Closed

Method ImageOps.pad doesn't copy palette of original image #6595

PososikTeam opened this issue Sep 18, 2022 · 1 comment · Fixed by #6596
Labels

Comments

@PososikTeam
Copy link
Contributor

I think i found a bug with ImageOps.pad function.

I have an image which mode is P and this image has a specific palette. So when you do pad for this image it will return an image that is displayed as completely black.

So this is origin image
spidy

And this image is after applying ImageOps.pad
pad_spidy

This images all have the same unique values of pixels - (array([0, 1, 2, 3, 4, 5], dtype=uint8), array([0, 1, 2, 3, 4, 5], dtype=uint8))

But when convert origin and pad image to rgb it will have different uniq value - (array([0, 1, 2, 3, 4, 5], dtype=uint8), array([ 0, 13, 35, 80, 83, 98, 148, 150, 171, 177, 212, 227, 255], dtype=uint8))

What did you do?

Download spidy image. It is image with mode P and has a specific palette. Read this image via Pillow then use ImageOps.pad and you will see full black image.

What did you expect to happen?

I expect to see pad colorful spidy image.

What actually happened?

In pad palette of origin image don't copy, so you see full black image after padding.

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 20.04
  • Python: 3.9
  • Pillow: 9.1.1

Reproduce code:

from PIL import Image, ImageOps
import matplotlib.pyplot as plt
import numpy as np

with Image.open("spidy.png") as origin_image:
    plt.imshow(origin_image)
    plt.show()

pad_image = ImageOps.pad(origin_image, (224, 224))
pad_image.save('pad_spidy.png')
plt.imshow(pad_image)
plt.show()

pad_uniq = np.unique(np.asarray(pad_image))
origin_uniq = np.unique(np.asarray(origin_image))

print(pad_uniq, origin_uniq)

pad_rgb_image = pad_image.convert('RGB')
origin_rgb_image = origin_image.convert('RGB')

pad_uniq_val = np.unique(np.asarray(pad_rgb_image))
origin_uniq_val = np.unique(np.asarray(origin_rgb_image))

print(pad_uniq_val, origin_uniq_val)
@PososikTeam
Copy link
Contributor Author

This PR #6596 solve this issue

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

Successfully merging a pull request may close this issue.

2 participants