Skip to content

Commit 64edb9c

Browse files
radarherehugovk
authored andcommitted
Fixed generated palettes
1 parent 8f1524e commit 64edb9c

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

Tests/images/palette_negative.png

16.7 KB
Loading

Tests/images/palette_sepia.png

19.9 KB
Loading

Tests/images/palette_wedge.png

16.7 KB
Loading

Tests/test_image_putpalette.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from PIL import Image, ImagePalette
44

5-
from .helper import assert_image_equal, hopper
5+
from .helper import assert_image_equal, assert_image_equal_tofile, hopper
66

77

88
def test_putpalette():
@@ -36,9 +36,15 @@ def palette(mode):
3636
def test_imagepalette():
3737
im = hopper("P")
3838
im.putpalette(ImagePalette.negative())
39+
assert_image_equal_tofile(im.convert("RGB"), "Tests/images/palette_negative.png")
40+
3941
im.putpalette(ImagePalette.random())
42+
4043
im.putpalette(ImagePalette.sepia())
44+
assert_image_equal_tofile(im.convert("RGB"), "Tests/images/palette_sepia.png")
45+
4146
im.putpalette(ImagePalette.wedge())
47+
assert_image_equal_tofile(im.convert("RGB"), "Tests/images/palette_wedge.png")
4248

4349

4450
def test_putpalette_with_alpha_values():

src/PIL/ImagePalette.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ def make_gamma_lut(exp):
204204

205205

206206
def negative(mode="RGB"):
207-
palette = list(range(256))
207+
palette = list(range(256 * len(mode)))
208208
palette.reverse()
209-
return ImagePalette(mode, palette * len(mode))
209+
return ImagePalette(mode, [i // len(mode) for i in palette])
210210

211211

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

220220

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

228225

229226
def wedge(mode="RGB"):
230-
return ImagePalette(mode, list(range(256)) * len(mode))
227+
palette = list(range(256 * len(mode)))
228+
return ImagePalette(mode, [i // len(mode) for i in palette])
231229

232230

233231
def load(filename):

0 commit comments

Comments
 (0)