Do not alter pixels outside mask when drawing text on an image with transparency

This commit is contained in:
Andrew Murray 2021-04-24 11:14:18 +10:00
parent ef9a8e5f7f
commit 8bc19ac78c
5 changed files with 5 additions and 2 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 350 B

View File

@ -236,7 +236,7 @@ class TestImagingPaste:
[ [
(127, 191, 254, 191), (127, 191, 254, 191),
(111, 207, 206, 110), (111, 207, 206, 110),
(255, 255, 255, 0) if mode == "RGBA" else (127, 254, 127, 0), (127, 254, 127, 0),
(207, 207, 239, 239), (207, 207, 239, 239),
(191, 191, 190, 191), (191, 191, 190, 191),
(207, 206, 111, 112), (207, 206, 111, 112),

View File

@ -131,6 +131,9 @@ class TestImageFont:
target = "Tests/images/transparent_background_text.png" target = "Tests/images/transparent_background_text.png"
assert_image_similar_tofile(im, target, 4.09) assert_image_similar_tofile(im, target, 4.09)
target = "Tests/images/transparent_background_text_L.png"
assert_image_similar_tofile(im.convert("L"), target, 0.01)
def test_textsize_equal(self): def test_textsize_equal(self):
im = Image.new(mode="RGB", size=(300, 100)) im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im) draw = ImageDraw.Draw(im)

View File

@ -436,7 +436,7 @@ fill_mask_L(
strcmp(imOut->mode, "La") == 0 || strcmp(imOut->mode, "La") == 0 ||
strcmp(imOut->mode, "LA") == 0 || strcmp(imOut->mode, "LA") == 0 ||
strcmp(imOut->mode, "PA") == 0) && strcmp(imOut->mode, "PA") == 0) &&
i != 3) { i != 3 && channel_mask != 0) {
channel_mask = channel_mask =
255 - (255 - channel_mask) * (1 - (255 - out[3]) / 255); 255 - (255 - channel_mask) * (1 - (255 - out[3]) / 255);
} }