From a4f0022a5aa5706df24d1c3e5a0eb775d9ecea70 Mon Sep 17 00:00:00 2001 From: Jason Date: Sun, 16 Jun 2024 17:01:11 -0400 Subject: [PATCH] Fix: AttributeError: 'FreeTypeFont' object has no attribute 'getsize' --- utils/fonts.py | 12 ++++++++++++ utils/imagenarator.py | 7 ++++--- video_creation/final_video.py | 12 ++++++------ 3 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 utils/fonts.py diff --git a/utils/fonts.py b/utils/fonts.py new file mode 100644 index 0000000..dc851a2 --- /dev/null +++ b/utils/fonts.py @@ -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 diff --git a/utils/imagenarator.py b/utils/imagenarator.py index 6ed22cf..14def21 100644 --- a/utils/imagenarator.py +++ b/utils/imagenarator.py @@ -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): diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 2011f04..70f39c4 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -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