Skip to content

Commit

Permalink
fix drawing text alpha on RGBA image on big-endian platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
nulano committed Nov 27, 2023
1 parent f3b3442 commit 0cef9f2
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
Binary file modified Tests/images/bitmap_font_blend.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 3 additions & 1 deletion src/PIL/ImageDraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import math
import numbers
import struct

from . import Image, ImageColor

Expand Down Expand Up @@ -542,7 +543,8 @@ def draw_text(ink, stroke_width=0, stroke_offset=None):
# 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:
Expand Down

0 comments on commit 0cef9f2

Please sign in to comment.