Skip to content

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

@PososikTeam

Description

@PososikTeam

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions