Fixed generated palettes

This commit is contained in:
Andrew Murray 2021-07-10 23:13:46 +10:00
parent d0a30ec369
commit a9372d5cf0
5 changed files with 13 additions and 9 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -2,7 +2,7 @@ import pytest
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():
@ -36,9 +36,15 @@ def test_putpalette():
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():

View File

@ -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"):
@ -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):