Skip to content

Commit

Permalink
Use transparency info key when converting to LA
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Mar 1, 2022
1 parent 5c62120 commit e2b007f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
4 changes: 4 additions & 0 deletions Tests/test_image_convert.py
Expand Up @@ -135,6 +135,10 @@ def test_trns_l(tmp_path):

f = str(tmp_path / "temp.png")

im_la = im.convert("LA")
assert "transparency" not in im_la.info
im_la.save(f)

im_rgb = im.convert("RGB")
assert im_rgb.info["transparency"] == (128, 128, 128) # undone
im_rgb.save(f)
Expand Down
4 changes: 3 additions & 1 deletion src/PIL/Image.py
Expand Up @@ -975,7 +975,9 @@ def convert_transparency(m, v):
delete_trns = False
# transparency handling
if has_transparency:
if self.mode in ("1", "L", "I", "RGB") and mode == "RGBA":
if (self.mode in ("1", "L", "I") and mode in ("LA", "RGBA")) or (
self.mode == "RGB" and mode == "RGBA"
):
# Use transparent conversion to promote from transparent
# color to an alpha channel.
new_im = self._new(
Expand Down
39 changes: 17 additions & 22 deletions src/libImaging/Convert.c
Expand Up @@ -1634,29 +1634,15 @@ ImagingConvertTransparent(Imaging imIn, const char *mode, int r, int g, int b) {
return (Imaging)ImagingError_ModeError();
}

if (!((strcmp(imIn->mode, "RGB") == 0 || strcmp(imIn->mode, "1") == 0 ||
strcmp(imIn->mode, "I") == 0 || strcmp(imIn->mode, "L") == 0) &&
strcmp(mode, "RGBA") == 0))
#ifdef notdef
{
return (Imaging)ImagingError_ValueError("conversion not supported");
}
#else
{
static char buf[100];
snprintf(
buf,
100,
"conversion from %.10s to %.10s not supported in convert_transparent",
imIn->mode,
mode);
return (Imaging)ImagingError_ValueError(buf);
}
#endif

if (strcmp(imIn->mode, "RGB") == 0) {
if (strcmp(imIn->mode, "RGB") == 0 && strcmp(mode, "RGBA") == 0) {
convert = rgb2rgba;
} else {
} else if ((strcmp(imIn->mode, "1") == 0 ||
strcmp(imIn->mode, "I") == 0 ||
strcmp(imIn->mode, "L") == 0
) && (
strcmp(mode, "RGBA") == 0 ||
strcmp(mode, "LA") == 0
)) {
if (strcmp(imIn->mode, "1") == 0) {
convert = bit2rgb;
} else if (strcmp(imIn->mode, "I") == 0) {
Expand All @@ -1665,6 +1651,15 @@ ImagingConvertTransparent(Imaging imIn, const char *mode, int r, int g, int b) {
convert = l2rgb;
}
g = b = r;
} else {
static char buf[100];
snprintf(
buf,
100,
"conversion from %.10s to %.10s not supported in convert_transparent",
imIn->mode,
mode);
return (Imaging)ImagingError_ValueError(buf);
}

imOut = ImagingNew2Dirty(mode, imOut, imIn);
Expand Down

0 comments on commit e2b007f

Please sign in to comment.