+2








902ff00cb0
Co-authored-by: Jason Cameron <git@jasoncameron.dev> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: RequieMa <57754488+RequieMa@users.noreply.github.com> Co-authored-by: prafful <praffulsharma1230@gmail.com> Co-authored-by: M. Sanaullah <thebolly@gmail.com> Co-authored-by: embee <emilien.bev.com@gmail.com> Co-authored-by: Rodrigo <55567123+rodrigodasilv@users.noreply.github.com> Co-authored-by: Emilien Bevierre <44171454+emilienbev@users.noreply.github.com> Co-authored-by: tkhmielnitzky <tkhmielnitzky@gmail.com> Co-authored-by: bnfone <89687390+bnfone@users.noreply.github.com> Co-authored-by: Cyteon <129582290+Cyteon@users.noreply.github.com>
75 lines
2.5 KiB
Python
75 lines
2.5 KiB
Python
import os
|
|
import re
|
|
import textwrap
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
from rich.progress import track
|
|
|
|
from TTS.engine_wrapper import process_text
|
|
from utils.fonts import getheight, getsize
|
|
from utils.id import extract_id
|
|
|
|
|
|
def draw_multiple_line_text(
|
|
image, text, font, text_color, padding, wrap=50, transparent=False
|
|
) -> None:
|
|
"""
|
|
Draw multiline text over given image
|
|
"""
|
|
draw = ImageDraw.Draw(image)
|
|
font_height = getheight(font, text)
|
|
image_width, image_height = image.size
|
|
lines = textwrap.wrap(text, width=wrap)
|
|
y = (image_height / 2) - (((font_height + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
|
|
for line in lines:
|
|
line_width, line_height = getsize(font, line)
|
|
if transparent:
|
|
shadowcolor = "black"
|
|
for i in range(1, 5):
|
|
draw.text(
|
|
((image_width - line_width) / 2 - i, y - i),
|
|
line,
|
|
font=font,
|
|
fill=shadowcolor,
|
|
)
|
|
draw.text(
|
|
((image_width - line_width) / 2 + i, y - i),
|
|
line,
|
|
font=font,
|
|
fill=shadowcolor,
|
|
)
|
|
draw.text(
|
|
((image_width - line_width) / 2 - i, y + i),
|
|
line,
|
|
font=font,
|
|
fill=shadowcolor,
|
|
)
|
|
draw.text(
|
|
((image_width - line_width) / 2 + i, y + i),
|
|
line,
|
|
font=font,
|
|
fill=shadowcolor,
|
|
)
|
|
draw.text(((image_width - line_width) / 2, y), line, font=font, fill=text_color)
|
|
y += line_height + padding
|
|
|
|
|
|
def imagemaker(theme, reddit_obj: dict, txtclr, padding=5, transparent=False) -> None:
|
|
"""
|
|
Render Images for video
|
|
"""
|
|
texts = reddit_obj["thread_post"]
|
|
reddit_id = extract_id(reddit_obj)
|
|
if transparent:
|
|
font = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), 100)
|
|
else:
|
|
font = ImageFont.truetype(os.path.join("fonts", "Roboto-Regular.ttf"), 100)
|
|
|
|
size = (1920, 1080)
|
|
|
|
for idx, text in track(enumerate(texts), "Rendering Image"):
|
|
image = Image.new("RGBA", size, theme)
|
|
text = process_text(text, False)
|
|
draw_multiple_line_text(image, text, font, txtclr, padding, wrap=30, transparent=transparent)
|
|
image.save(f"assets/temp/{reddit_id}/png/img{idx}.png")
|