Skip to content

Commit 0465627

Browse files
committed
Fill alpha channel when quantizing RGB images
1 parent cee238b commit 0465627

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
@@ -116,6 +116,15 @@ def test_quantize_kmeans(method: Image.Quantize) -> None:
116116
im.quantize(kmeans=-1, method=method)
117117

118118

119+
@skip_unless_feature("libimagequant")
120+
def test_resize() -> None:
121+
im = hopper().resize((100, 100))
122+
converted = im.quantize(100, Image.Quantize.LIBIMAGEQUANT)
123+
colors = converted.getcolors()
124+
assert colors is not None
125+
assert len(colors) == 100
126+
127+
119128
def test_colors() -> None:
120129
im = hopper()
121130
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)