Captcha: Fix text layer location calculation

ImageFont.getbbox() returns a tuple of coordinates for the text's
bounding box, (left, top, right, bottom).  Calculate the left margin
available in the image to place the text layer by subtracting the right
coordinate from the image width and adding it to the text's own left
margin. Similarly, to find the available top margin, subtract the bottom
coordinate from the image height and add it to the text's own top
margin.

Change-Id: I738816114be4c0a746733dc8e7ce5789aed57833
This commit is contained in:
0weng 2024-11-19 11:33:26 -08:00 committed by Clark Boylan
parent b16ec64fa4
commit fa448c3de9

View File

@ -152,8 +152,14 @@ class TextLayer(Layer):
text_image = Image.new('L', image.size, 0)
draw = ImageDraw.Draw(text_image)
text_size = self.font.getbbox(self.text)
x = int((image.size[0] - text_size[0]) * self.alignment[0] + 0.5)
y = int((image.size[1] - text_size[1]) * self.alignment[1] + 0.5)
x = int(
(image.size[0] - text_size[2] + text_size[0]) * self.alignment[0]
+ 0.5
)
y = int(
(image.size[1] - text_size[3] + text_size[1]) * self.alignment[1]
+ 0.5
)
draw.text((x, y), self.text, font=self.font,
fill=255 - self.transparency)