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

Updates for ImagePalette channel order #5599

Merged
merged 3 commits into from Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added Tests/images/palette_negative.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/palette_sepia.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/palette_wedge.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Tests/test_file_gif.py
Expand Up @@ -833,7 +833,7 @@ def test_palette_save_ImagePalette(tmp_path):

with Image.open(out) as reloaded:
im.putpalette(palette)
assert_image_equal(reloaded, im)
assert_image_equal(reloaded.convert("RGB"), im.convert("RGB"))


def test_save_I(tmp_path):
Expand Down
8 changes: 7 additions & 1 deletion Tests/test_image_putpalette.py
Expand Up @@ -2,7 +2,7 @@

from PIL import Image, ImagePalette

from .helper import assert_image_equal, hopper
from .helper import assert_image_equal, assert_image_equal_tofile, hopper


def test_putpalette():
Expand Down Expand Up @@ -36,9 +36,15 @@ def palette(mode):
def test_imagepalette():
im = hopper("P")
im.putpalette(ImagePalette.negative())
assert_image_equal_tofile(im.convert("RGB"), "Tests/images/palette_negative.png")

im.putpalette(ImagePalette.random())

im.putpalette(ImagePalette.sepia())
assert_image_equal_tofile(im.convert("RGB"), "Tests/images/palette_sepia.png")

im.putpalette(ImagePalette.wedge())
assert_image_equal_tofile(im.convert("RGB"), "Tests/images/palette_wedge.png")


def test_putpalette_with_alpha_values():
Expand Down
4 changes: 0 additions & 4 deletions docs/reference/ImagePalette.rst
Expand Up @@ -9,10 +9,6 @@ represent the color palette of palette mapped images.

.. note::

This module was never well-documented. It hasn't changed since 2001,
though, so it's probably safe for you to read the source code and puzzle
out the internals if you need to.

The :py:class:`~PIL.ImagePalette.ImagePalette` class has several methods,
but they are all marked as "experimental." Read that as you will. The
``[source]`` link is there for a reason.
Expand Down
10 changes: 1 addition & 9 deletions src/PIL/GifImagePlugin.py
Expand Up @@ -396,15 +396,7 @@ def _normalize_palette(im, palette, info):
if isinstance(palette, (bytes, bytearray, list)):
source_palette = bytearray(palette[:768])
if isinstance(palette, ImagePalette.ImagePalette):
source_palette = bytearray(
itertools.chain.from_iterable(
zip(
palette.palette[:256],
palette.palette[256:512],
palette.palette[512:768],
)
)
)
source_palette = bytearray(palette.palette)

if im.mode == "P":
if not source_palette:
Expand Down
22 changes: 10 additions & 12 deletions src/PIL/ImagePalette.py
Expand Up @@ -25,12 +25,12 @@ class ImagePalette:
"""
Color palette for palette mapped images

:param mode: The mode to use for the Palette. See:
:param mode: The mode to use for the palette. See:
:ref:`concept-modes`. Defaults to "RGB"
:param palette: An optional palette. If given, it must be a bytearray,
an array or a list of ints between 0-255. The list must be aligned
by channel (All R values must be contiguous in the list before G
and B values.) Defaults to 0 through 255 per channel.
an array or a list of ints between 0-255. The list must consist of
all channels for one color followed by the next color (e.g. RGBRGBRGB).
Defaults to an empty palette.
:param size: An optional palette size. If given, an error is raised
if ``palette`` is not of equal length.
"""
Expand Down Expand Up @@ -204,9 +204,9 @@ def make_gamma_lut(exp):


def negative(mode="RGB"):
palette = list(range(256))
palette = list(range(256 * len(mode)))
palette.reverse()
return ImagePalette(mode, palette * len(mode))
return ImagePalette(mode, [i // len(mode) for i in palette])


def random(mode="RGB"):
Expand All @@ -219,15 +219,13 @@ def random(mode="RGB"):


def sepia(white="#fff0c0"):
r, g, b = ImageColor.getrgb(white)
r = make_linear_lut(0, r)
g = make_linear_lut(0, g)
b = make_linear_lut(0, b)
return ImagePalette("RGB", r + g + b)
bands = [make_linear_lut(0, band) for band in ImageColor.getrgb(white)]
return ImagePalette("RGB", [bands[i % 3][i // 3] for i in range(256 * 3)])


def wedge(mode="RGB"):
return ImagePalette(mode, list(range(256)) * len(mode))
palette = list(range(256 * len(mode)))
return ImagePalette(mode, [i // len(mode) for i in palette])


def load(filename):
Expand Down