From 8bc19ac78cfa9d434e773279d4f8059ecd28116f Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 24 Apr 2021 11:14:18 +1000 Subject: [PATCH] Do not alter pixels outside mask when drawing text on an image with transparency --- Tests/images/transparent_background_text.png | Bin 1271 -> 1326 bytes .../images/transparent_background_text_L.png | Bin 0 -> 350 bytes Tests/test_image_paste.py | 2 +- Tests/test_imagefont.py | 3 +++ src/libImaging/Paste.c | 2 +- 5 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 Tests/images/transparent_background_text_L.png diff --git a/Tests/images/transparent_background_text.png b/Tests/images/transparent_background_text.png index 40acd92b62243953ebc0910629922eef112424e1..8ddd65cc68b5b65bdeb47a9701c6dcf50c564b36 100644 GIT binary patch delta 1175 zcmV;I1ZeyB39br|By06aL_t(|obB6NsAgjv$MN5PLdvAcPI)Bj-a=G>&1K3x_dWFq$zk7``svg?HY0-@W&H+D_k3 zt=fD2pJ%P#UhVz7&wAF{iIZUj7Jq45z&zj={<3V8?f-8^aeaW}fZv+_GbdgX ziKX6V^wStPx9u1I-M0NNwC%rqm_SwaUR7;hRh8u8QMVr#;0m^OdRy&`j@nSys2`)a zK2`N>Rn189<|Iw;OLEU3=a8miyM7v1)dRqsBx|bbk+%JdfMb%JTva!ZI)CK9K~*gW z7ACo%sy^?iJ)GoWV0My+{=?Vk*CfCF-68bf)z=J5lUkc(p9$*wCi!BTviu!3Hk&jS z(=uQIOMth5AA$A2ICqac^MU7p@0;y;VEzaWz14sPEC*J!)%Qu40jr-p!0XLz z<)-HQ-GK*z4Zv#P=wW{dV1Fs_0kF9_<|RN)q3gyZ8-_i<)XUfhEa288OOsrhWVa-T zCV48!lfY5qK7W@yOOmWeaz~Oqo9(tFE1KFc)}}R!3b0+01xe-uJ2mxfW?22^sj8!z z^{6$#kx5=l@RF)%12-N81I! zE5P<$j&mrm3g|g#0vFN8Q?1F`19O0nfL*%Oc4=yRP4#?Tj&VG2XY=&};CA3HU~#kW zCSbuR@&F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Zmjhn`TS&2SS3xG>Vzxs!B-5~H9=^}eO zw^x<-m;pRSx_{^g3iTh6W-gmaGoAfy?!OFr@q-_9r6}Y*B{TG*nOGq=x^}uzxu77rh)k!=fq$K~&I_>S~Bh9cz zO4^=?4)thyqUd>EeRI_Uy>gA7|koq zBn@iah-U=&qwPq`%?5s`(>6@wSRp;?vc0PjNd@gL3CC+cX*u+HkB>86`+zwO+UL;z zauCw&Vt*=dF7PWUX-(ee`J`psaE0-->UYv0`b$aM1X6yjk#${ZbRA`+#XBZKnbE18X}x#v#&D?~_8^I^agqb4pdm(~1vBIkhr(17}tjiu7N- z9LA2BN8bpD*wKHwfw{%X{E z(w)-UnM)P%v|=r3skt7wEXOi8$9Nk@zui!48Yw})oALozs!o0z$3oQCAA|}=JZ|D3>jEILL;-`ohj)iVY4#@0&SCSl{?c>J_6bO`whgnT2W^t4~z@>APyPv*w)tng6`kvut#~ z?|b2T=iFM^{M}0*Dy&{z`6%Pw|MHc`0&VXJEIsn4Cckj;PMLOt-DUkg;z#Rpza+o= z?I${U>5*qwE&s4uybKGo+hZ#|`RzeX_qnw%-HWbAE^J-vD2qyDDs{g>^Y);a0?w`rx?=F93{Pq_Lz@61fO z(jSW~mxf19EZ$Z&>w`}F-=i~Us7;ryS7h7xuiR|C>GXy*i#|V`J=f;R+Xr`IramjX nerBiP%NZw`Kmmge8s7h4=D6sq_AT$-OOTwWtDnm{r-UW|IF_F* literal 0 HcmV?d00001 diff --git a/Tests/test_image_paste.py b/Tests/test_image_paste.py index 3740fbcdc..1d3ca8135 100644 --- a/Tests/test_image_paste.py +++ b/Tests/test_image_paste.py @@ -236,7 +236,7 @@ class TestImagingPaste: [ (127, 191, 254, 191), (111, 207, 206, 110), - (255, 255, 255, 0) if mode == "RGBA" else (127, 254, 127, 0), + (127, 254, 127, 0), (207, 207, 239, 239), (191, 191, 190, 191), (207, 206, 111, 112), diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 883c14170..e843351ab 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -131,6 +131,9 @@ class TestImageFont: target = "Tests/images/transparent_background_text.png" 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): im = Image.new(mode="RGB", size=(300, 100)) draw = ImageDraw.Draw(im) diff --git a/src/libImaging/Paste.c b/src/libImaging/Paste.c index 03b17f571..a1bf18a92 100644 --- a/src/libImaging/Paste.c +++ b/src/libImaging/Paste.c @@ -436,7 +436,7 @@ fill_mask_L( strcmp(imOut->mode, "La") == 0 || strcmp(imOut->mode, "LA") == 0 || strcmp(imOut->mode, "PA") == 0) && - i != 3) { + i != 3 && channel_mask != 0) { channel_mask = 255 - (255 - channel_mask) * (1 - (255 - out[3]) / 255); }