Skip to content

Commit 3152da4

Browse files
committed
Allow alpha_composite to use LA images
1 parent 5554e77 commit 3152da4

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

Tests/test_image.py

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

401+
def test_alpha_composite_la(self) -> None:
402+
# Arrange
403+
expected_colors = sorted(
404+
[
405+
(3300, (255, 255)),
406+
(1156, (170, 192)),
407+
(1122, (128, 255)),
408+
(1089, (0, 0)),
409+
(1122, (255, 128)),
410+
(1122, (0, 128)),
411+
(1089, (0, 255)),
412+
]
413+
)
414+
415+
dst = Image.new("LA", size=(100, 100), color=(0, 255))
416+
draw = ImageDraw.Draw(dst)
417+
draw.rectangle((0, 33, 100, 66), fill=(0, 128))
418+
draw.rectangle((0, 67, 100, 100), fill=(0, 0))
419+
src = Image.new("LA", size=(100, 100), color=(255, 255))
420+
draw = ImageDraw.Draw(src)
421+
draw.rectangle((33, 0, 66, 100), fill=(255, 128))
422+
draw.rectangle((67, 0, 100, 100), fill=(255, 0))
423+
424+
# Act
425+
img = Image.alpha_composite(dst, src)
426+
427+
# Assert
428+
img_colors = img.getcolors()
429+
assert img_colors is not None
430+
assert sorted(img_colors) == expected_colors
431+
401432
def test_alpha_inplace(self) -> None:
402433
src = Image.new("RGBA", (128, 128), "blue")
403434

src/PIL/Image.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3588,9 +3588,8 @@ def alpha_composite(im1: Image, im2: Image) -> Image:
35883588
"""
35893589
Alpha composite im2 over im1.
35903590
3591-
:param im1: The first image. Must have mode RGBA.
3592-
:param im2: The second image. Must have mode RGBA, and the same size as
3593-
the first image.
3591+
:param im1: The first image. Must have mode RGBA or LA.
3592+
:param im2: The second image. Must have the same mode and size as the first image.
35943593
:returns: An :py:class:`~PIL.Image.Image` object.
35953594
"""
35963595

src/libImaging/AlphaComposite.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ ImagingAlphaComposite(Imaging imDst, Imaging imSrc) {
2525
int x, y;
2626

2727
/* Check arguments */
28-
if (!imDst || !imSrc || strcmp(imDst->mode, "RGBA")) {
28+
if (!imDst || !imSrc ||
29+
(strcmp(imDst->mode, "RGBA") && strcmp(imDst->mode, "LA"))) {
2930
return ImagingError_ModeError();
3031
}
3132

0 commit comments

Comments
 (0)