use draw.textbox instead of draw.textsize

This commit is contained in:
j 2023-08-25 17:07:08 +02:00
parent 31a491570c
commit d5635508bb

View file

@ -22,11 +22,17 @@ ZONE_INDEX = [
] ]
] ]
def textsize(draw, text, font):
left, top, right, bottom = draw.textbbox((0, 0), text, font=font)
return (right, bottom)
def drawText(image, position, text, font_file, font_size, color): def drawText(image, position, text, font_file, font_size, color):
draw = ImageDraw.Draw(image) draw = ImageDraw.Draw(image)
font = ImageFont.truetype(font_file, font_size, encoding='unic') font = ImageFont.truetype(font_file, font_size, encoding='unic')
draw.text(position, text, fill=color, font=font) draw.text(position, text, fill=color, font=font)
size = draw.textsize(text, font=font) size = textsize(draw, text, font)
version = getattr(Image, 'PILLOW_VERSION', None) version = getattr(Image, 'PILLOW_VERSION', None)
if version and version > '2.1.0' and version < '2.6.1': if version and version > '2.1.0' and version < '2.6.1':
offset = font.getoffset(text) offset = font.getoffset(text)
@ -148,7 +154,7 @@ def getRGB(hsl):
def getTextSize(image, text, font_file, font_size): def getTextSize(image, text, font_file, font_size):
draw = ImageDraw.Draw(image) draw = ImageDraw.Draw(image)
font = ImageFont.truetype(font_file, font_size, encoding='unic') font = ImageFont.truetype(font_file, font_size, encoding='unic')
size = draw.textsize(text, font=font) size = textsize(draw, text, font)
version = getattr(Image, 'PILLOW_VERSION', None) version = getattr(Image, 'PILLOW_VERSION', None)
if version and version > '2.1.0' and version < '2.6.1': if version and version > '2.1.0' and version < '2.6.1':
offset = font.getoffset(text) offset = font.getoffset(text)
@ -168,7 +174,7 @@ def wrapText(text, max_width, max_lines, font_file, font_size):
return min_width return min_width
def get_width(string): def get_width(string):
return draw.textsize(string, font=font)[0] return textsize(draw, text, font)[0]
image = Image.new('RGB', (1, 1)) image = Image.new('RGB', (1, 1))
draw = ImageDraw.Draw(image) draw = ImageDraw.Draw(image)