Fix: AttributeError: 'FreeTypeFont' object has no attribute 'getsize'

pull/2060/head
Jason 5 months ago
parent 901ad75e0b
commit a4f0022a5a

@ -0,0 +1,12 @@
from PIL.ImageFont import ImageFont, FreeTypeFont
def getsize(font: ImageFont | FreeTypeFont, text: str):
left, top, right, bottom = font.getbbox(text)
width = right - left
height = bottom - top
return width, height
def getheight(font: ImageFont | FreeTypeFont, text: str):
_, height = getsize(font, text)
return height

@ -6,6 +6,7 @@ from PIL import Image, ImageDraw, ImageFont
from rich.progress import track
from TTS.engine_wrapper import process_text
from utils.fonts import getsize, getheight
def draw_multiple_line_text(
@ -15,12 +16,12 @@ def draw_multiple_line_text(
Draw multiline text over given image
"""
draw = ImageDraw.Draw(image)
Fontperm = font.getsize(text)
font_height = getheight(font, text)
image_width, image_height = image.size
lines = textwrap.wrap(text, width=wrap)
y = (image_height / 2) - (((Fontperm[1] + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
y = (image_height / 2) - (((font_height + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
for line in lines:
line_width, line_height = font.getsize(line)
line_width, line_height = getsize(font, line)
if transparent:
shadowcolor = "black"
for i in range(1, 5):

@ -20,6 +20,7 @@ from rich.progress import track
from utils import settings
from utils.cleanup import cleanup
from utils.console import print_step, print_substep
from utils.fonts import getheight
from utils.thumbnail import create_thumbnail
from utils.videos import save_data
@ -117,7 +118,7 @@ def create_fancy_thumbnail(image, text, text_color, padding, wrap=35):
lines = textwrap.wrap(text, width=wrap)
y = (
(image_height / 2)
- (((font.getsize(text)[1] + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
- (((getheight(font, text) + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
+ 30
)
draw = ImageDraw.Draw(image)
@ -137,7 +138,7 @@ def create_fancy_thumbnail(image, text, text_color, padding, wrap=35):
font = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), font_title_size)
y = (
(image_height / 2)
- (((font.getsize(text)[1] + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
- (((getheight(font, text) + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
+ 35
)
elif len(lines) == 4:
@ -146,7 +147,7 @@ def create_fancy_thumbnail(image, text, text_color, padding, wrap=35):
font = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), font_title_size)
y = (
(image_height / 2)
- (((font.getsize(text)[1] + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
- (((getheight(font, text) + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
+ 40
)
elif len(lines) > 4:
@ -155,14 +156,13 @@ def create_fancy_thumbnail(image, text, text_color, padding, wrap=35):
font = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), font_title_size)
y = (
(image_height / 2)
- (((font.getsize(text)[1] + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
- (((getheight(font, text) + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
+ 30
)
for line in lines:
_, line_height = font.getsize(line)
draw.text((120, y), line, font=font, fill=text_color, align="left")
y += line_height + padding
y += getheight(font, line) + padding
return image

Loading…
Cancel
Save