From fa448c3de9dce6942643b82a91fbe929fb6b68a7 Mon Sep 17 00:00:00 2001 From: 0weng Date: Tue, 19 Nov 2024 11:33:26 -0800 Subject: [PATCH] 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 --- lodgeit/lib/captcha.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lodgeit/lib/captcha.py b/lodgeit/lib/captcha.py index 9fe2b62..7e9d0fa 100644 --- a/lodgeit/lib/captcha.py +++ b/lodgeit/lib/captcha.py @@ -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)