Use transparency info key when converting to LA

This commit is contained in:
Andrew Murray 2022-03-01 20:25:25 +11:00
parent 5c6212052c
commit e2b007f0c2
3 changed files with 24 additions and 23 deletions

View File

@ -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)

View File

@ -975,7 +975,9 @@ class Image:
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(

View File

@ -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) {
@ -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);