Skip to content

Commit

Permalink
Fixed generated palettes
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jul 29, 2021
1 parent d0a30ec commit a9372d5
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
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.
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
14 changes: 6 additions & 8 deletions src/PIL/ImagePalette.py
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

0 comments on commit a9372d5

Please sign in to comment.