Skip to content

Commit 71e6660

Browse files
authored
Fill alpha channel when quantizing RGB images (python-pillow#9133)
2 parents 8c42abd + 0465627 commit 71e6660

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

Tests/test_image_quantize.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ def test_quantize_kmeans(method: Image.Quantize) -> None:
118118
im.quantize(kmeans=-1, method=method)
119119

120120

121+
@skip_unless_feature("libimagequant")
122+
def test_resize() -> None:
123+
im = hopper().resize((100, 100))
124+
converted = im.quantize(100, Image.Quantize.LIBIMAGEQUANT)
125+
colors = converted.getcolors()
126+
assert colors is not None
127+
assert len(colors) == 100
128+
129+
121130
def test_colors() -> None:
122131
im = hopper()
123132
colors = 2

src/libImaging/Quant.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,19 +1745,23 @@ ImagingQuantize(Imaging im, int colors, int mode, int kmeans) {
17451745
for (i = y = 0; y < im->ysize; y++) {
17461746
for (x = 0; x < im->xsize; x++, i++) {
17471747
p[i].v = im->image32[y][x];
1748-
if (withAlpha && p[i].c.a == 0) {
1749-
if (transparency == 0) {
1750-
transparency = 1;
1751-
r = p[i].c.r;
1752-
g = p[i].c.g;
1753-
b = p[i].c.b;
1754-
} else {
1755-
/* Set all subsequent transparent pixels
1756-
to the same colour as the first */
1757-
p[i].c.r = r;
1758-
p[i].c.g = g;
1759-
p[i].c.b = b;
1748+
if (withAlpha) {
1749+
if (p[i].c.a == 0) {
1750+
if (transparency == 0) {
1751+
transparency = 1;
1752+
r = p[i].c.r;
1753+
g = p[i].c.g;
1754+
b = p[i].c.b;
1755+
} else {
1756+
/* Set all subsequent transparent pixels
1757+
to the same colour as the first */
1758+
p[i].c.r = r;
1759+
p[i].c.g = g;
1760+
p[i].c.b = b;
1761+
}
17601762
}
1763+
} else {
1764+
p[i].c.a = 255;
17611765
}
17621766
}
17631767
}

0 commit comments

Comments
 (0)