Skip to content

Commit 9a2c516

Browse files
authored
Allow alpha_composite to use LA images (#9066)
2 parents bde9075 + 7ec13fe commit 9a2c516

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

Tests/test_image.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,37 @@ def test_alpha_composite(self) -> None:
388388
assert img_colors is not None
389389
assert sorted(img_colors) == expected_colors
390390

391+
def test_alpha_composite_la(self) -> None:
392+
# Arrange
393+
expected_colors = sorted(
394+
[
395+
(3300, (255, 255)),
396+
(1156, (170, 192)),
397+
(1122, (128, 255)),
398+
(1089, (0, 0)),
399+
(1122, (255, 128)),
400+
(1122, (0, 128)),
401+
(1089, (0, 255)),
402+
]
403+
)
404+
405+
dst = Image.new("LA", size=(100, 100), color=(0, 255))
406+
draw = ImageDraw.Draw(dst)
407+
draw.rectangle((0, 33, 100, 66), fill=(0, 128))
408+
draw.rectangle((0, 67, 100, 100), fill=(0, 0))
409+
src = Image.new("LA", size=(100, 100), color=(255, 255))
410+
draw = ImageDraw.Draw(src)
411+
draw.rectangle((33, 0, 66, 100), fill=(255, 128))
412+
draw.rectangle((67, 0, 100, 100), fill=(255, 0))
413+
414+
# Act
415+
img = Image.alpha_composite(dst, src)
416+
417+
# Assert
418+
img_colors = img.getcolors()
419+
assert img_colors is not None
420+
assert sorted(img_colors) == expected_colors
421+
391422
def test_alpha_inplace(self) -> None:
392423
src = Image.new("RGBA", (128, 128), "blue")
393424

src/PIL/Image.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3570,9 +3570,8 @@ def alpha_composite(im1: Image, im2: Image) -> Image:
35703570
"""
35713571
Alpha composite im2 over im1.
35723572
3573-
:param im1: The first image. Must have mode RGBA.
3574-
:param im2: The second image. Must have mode RGBA, and the same size as
3575-
the first image.
3573+
:param im1: The first image. Must have mode RGBA or LA.
3574+
:param im2: The second image. Must have the same mode and size as the first image.
35763575
:returns: An :py:class:`~PIL.Image.Image` object.
35773576
"""
35783577

src/libImaging/AlphaComposite.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ ImagingAlphaComposite(Imaging imDst, Imaging imSrc) {
2525
int x, y;
2626

2727
/* Check arguments */
28-
if (!imDst || !imSrc || strcmp(imDst->mode, "RGBA") ||
29-
imDst->type != IMAGING_TYPE_UINT8 || imDst->bands != 4) {
28+
if (!imDst || !imSrc ||
29+
(strcmp(imDst->mode, "RGBA") && strcmp(imDst->mode, "LA"))) {
3030
return ImagingError_ModeError();
3131
}
3232

33-
if (strcmp(imDst->mode, imSrc->mode) || imDst->type != imSrc->type ||
34-
imDst->bands != imSrc->bands || imDst->xsize != imSrc->xsize ||
33+
if (strcmp(imDst->mode, imSrc->mode) || imDst->xsize != imSrc->xsize ||
3534
imDst->ysize != imSrc->ysize) {
3635
return ImagingError_Mismatch();
3736
}

0 commit comments

Comments
 (0)