Skip to content

Commit

Permalink
Support conversion from RGB to La
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Mar 26, 2024
1 parent ab8f465 commit e79d174
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Tests/test_image_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ def test_trns_RGB(tmp_path: Path) -> None:
assert "transparency" not in im_la.info
im_la.save(f)

im_la = im.convert("La")
assert "transparency" not in im_la.info
assert im_la.getpixel((0, 0)) == (0, 0)

im_p = im.convert("P")
assert "transparency" in im_p.info
im_p.save(f)
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ def convert_transparency(m, v):
# transparency handling
if has_transparency:
if (self.mode in ("1", "L", "I", "I;16") and mode in ("LA", "RGBA")) or (
self.mode == "RGB" and mode in ("LA", "RGBa", "RGBA")
self.mode == "RGB" and mode in ("La", "LA", "RGBa", "RGBA")
):
# Use transparent conversion to promote from transparent
# color to an alpha channel.
Expand Down
8 changes: 6 additions & 2 deletions src/libImaging/Convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ rgba2rgb_(UINT8 *out, const UINT8 *in, int xsize) {
/*
* Conversion of RGB + single transparent color either to
* RGBA or LA, where any pixel matching the color will have the alpha channel set to 0, or
* RGBa, where any pixel matching the color will have all channels set to 0
* RGBa or La, where any pixel matching the color will have all channels set to 0
*/

static void
Expand Down Expand Up @@ -942,6 +942,7 @@ static struct {
{"RGB", "1", rgb2bit},
{"RGB", "L", rgb2l},
{"RGB", "LA", rgb2la},
{"RGB", "La", rgb2la},
{"RGB", "I", rgb2i},
{"RGB", "F", rgb2f},
{"RGB", "BGR;15", rgb2bgr15},
Expand Down Expand Up @@ -1698,9 +1699,12 @@ ImagingConvertTransparent(Imaging imIn, const char *mode, int r, int g, int b) {
if (strcmp(mode, "RGBa") == 0) {
premultiplied = 1;
}
} else if (strcmp(imIn->mode, "RGB") == 0 && strcmp(mode, "LA") == 0) {
} else if (strcmp(imIn->mode, "RGB") == 0 && (strcmp(mode, "LA") == 0 || strcmp(mode, "La") == 0)) {
convert = rgb2la;
source_transparency = 1;
if (strcmp(mode, "La") == 0) {
premultiplied = 1;
}
} else if ((strcmp(imIn->mode, "1") == 0 ||
strcmp(imIn->mode, "I") == 0 ||
strcmp(imIn->mode, "I;16") == 0 ||
Expand Down

0 comments on commit e79d174

Please sign in to comment.