captcha(-old).py: Support Pillow 10

getsize() function was removed in version 10

Bug: T354099
Change-Id: I019a5a89de4340d73a938c907c0a6f5cc22a659c
This commit is contained in:
Reedy 2024-01-02 02:46:20 +00:00
parent 2b15ffbb94
commit fbf8d90063
2 changed files with 22 additions and 2 deletions

View file

@ -46,6 +46,11 @@ except:
nonalpha = re.compile('[^a-z]') # regex to test for suitability of words
# Pillow 9.2 added getbbox to replace getsize, and getsize() was removed in Pillow 10
# https://pillow.readthedocs.io/en/stable/releasenotes/10.0.0.html#font-size-and-offset-methods
# We don't have a requirements.txt, and therefore don't declare any specific supported or min version...
IMAGEFONT_HAS_GETBBOX = hasattr(ImageFont.ImageFont, "getbbox")
# Does X-axis wobbly copy, sandwiched between two rotates
def wobbly_copy(src, wob, col, scale, ang):
x, y = src.size
@ -79,8 +84,13 @@ def gen_captcha(text, fontname, fontsize, file_name):
fgcolor = 0xffffff
# create a font object
font = ImageFont.truetype(fontname,fontsize)
# determine dimensions of the text
dim = font.getsize(text)
if IMAGEFONT_HAS_GETBBOX:
dim = font.getbbox(text)[2:]
else:
dim = font.getsize(text)
# create a new image significantly larger that the text
edge = max(dim[0], dim[1]) + 2*min(dim[0], dim[1])
im = Image.new('RGB', (edge, edge), bgcolor)

View file

@ -47,6 +47,11 @@ except:
nonalpha = re.compile('[^a-z]') # regex to test for suitability of words
# Pillow 9.2 added getbbox to replace getsize, and getsize() was removed in Pillow 10
# https://pillow.readthedocs.io/en/stable/releasenotes/10.0.0.html#font-size-and-offset-methods
# We don't have a requirements.txt, and therefore don't declare any specific supported or min version...
IMAGEFONT_HAS_GETBBOX = hasattr(ImageFont.ImageFont, "getbbox")
# Does X-axis wobbly copy, sandwiched between two rotates
def wobbly_copy(src, wob, col, scale, ang):
x, y = src.size
@ -80,8 +85,13 @@ def gen_captcha(text, fontname, fontsize, file_name):
fgcolor = 0xffffff
# create a font object
font = ImageFont.truetype(fontname,fontsize)
# determine dimensions of the text
dim = font.getsize(text)
if IMAGEFONT_HAS_GETBBOX:
dim = font.getbbox(text)[2:]
else:
dim = font.getsize(text)
# create a new image significantly larger that the text
edge = max(dim[0], dim[1]) + 2*min(dim[0], dim[1])
im = Image.new('RGB', (edge, edge), bgcolor)