From 0cef9f251cd4deda5155d93f2c77ed3176f278ef Mon Sep 17 00:00:00 2001 From: Nulano Date: Mon, 27 Nov 2023 16:42:45 +0100 Subject: [PATCH] fix drawing text alpha on RGBA image on big-endian platforms --- Tests/images/bitmap_font_blend.png | Bin 364 -> 387 bytes Tests/test_imagefont.py | 2 +- src/PIL/ImageDraw.py | 4 +++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Tests/images/bitmap_font_blend.png b/Tests/images/bitmap_font_blend.png index ac97357ab3f99dfcd27e389fc55fa346729a3558..a5acf3667400c8a37fb3a7c99c21b13ca65dd60b 100644 GIT binary patch literal 387 zcmeAS@N?(olHy`uVBq!ia0vp^4M3d0!3HF+R#ka2Ffe*~x;TbZ%z1lvVWE?wh{MGR z3wuf$IvoSpm2{5;dFDhp1h6v+Pv(1(e3|+58NX-q9)Eu?Tt4s4e#zt>@lA2z{0E|# zI)okOFg#)rP;ancJi@8q$54nWl$&l}@Yl{hxv%|h{(kTG^|#+oyI23?&kU=4W^Br_ zm_Sfy`^>-b2d@AA7`gdF!{zg-ulDfbbPYkXP`z>I%WsB1`DXZi2sX2Rv9IWs?&;BT Qz`$kjboFyt=akR{0G_ac+yDRo literal 364 zcmeAS@N?(olHy`uVBq!ia0vp^4M3d0!2~3uB;Lv~FfbZ>x;TbZ%z1lfV=j}UfI}eP zPM&fHC#MHi92t#QrcSuT#8@g|QRu|}rSRN|@J}y2Kg93ZF7-0|(B8~>&qL>Im3wek zltG&zjA0E!1mgy#1l9vw4JZQgi{26 z3tWEjzsc|RU#G=;+TP!Pec`7WUWX7ikM%&?efB5yTN$zY1ZKcD_Jb>gwx@Vi2?9fr N!PC{xWt~$(69Bm%cTNBR diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 859e39c1c..95e06227a 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -862,7 +862,7 @@ def test_bitmap_blend(layout_engine, embedded_color): "Tests/fonts/EBDTTestFont.ttf", size=64, layout_engine=layout_engine ) - im = Image.new("RGB", (128, 96), "white") + im = Image.new("RGBA", (128, 96), "white") d = ImageDraw.Draw(im) d.text((16, 16), "AA", font=font, embedded_color=embedded_color, fill="#8E2F52") diff --git a/src/PIL/ImageDraw.py b/src/PIL/ImageDraw.py index fbf320d72..4d5919f22 100644 --- a/src/PIL/ImageDraw.py +++ b/src/PIL/ImageDraw.py @@ -32,6 +32,7 @@ import math import numbers +import struct from . import Image, ImageColor @@ -542,7 +543,8 @@ class ImageDraw: # font.getmask2(mode="RGBA") returns color in RGB bands and mask in A # extract mask and set text alpha color, mask = mask, mask.getband(3) - color.fillband(3, (ink >> 24) & 0xFF) + ink_alpha = struct.pack("=i", ink)[3] + color.fillband(3, ink_alpha) x, y = coord self.im.paste(color, (x, y, x + mask.size[0], y + mask.size[1]), mask) else: