When converting to P, restrict colors to palette size

This commit is contained in:
Andrew Murray 2022-02-16 11:01:00 +11:00
parent 948c064b28
commit 54cb09d8b4
2 changed files with 17 additions and 2 deletions

View File

@ -108,3 +108,18 @@ def test_palette(method, color):
converted = im.quantize(method=method)
converted_px = converted.load()
assert converted_px[0, 0] == converted.palette.colors[color]
def test_small_palette():
# Arrange
im = hopper()
colors = (255, 0, 0, 0, 0, 255)
p = Image.new("P", (1, 1))
p.putpalette(colors)
# Act
im = im.quantize(palette=p)
# Assert
assert len(im.getcolors()) == 2

View File

@ -194,7 +194,7 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b) {
dmax = (unsigned int)~0;
for (i = 0; i < 256; i++) {
for (i = 0; i < palette->size; i++) {
int r, g, b;
unsigned int tmin, tmax;
@ -227,7 +227,7 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b) {
d[i] = (unsigned int)~0;
}
for (i = 0; i < 256; i++) {
for (i = 0; i < palette->size; i++) {
if (dmin[i] <= dmax) {
int rd, gd, bd;
int ri, gi, bi;