parent
64bf647de9
commit
39412e2a35
@ -0,0 +1,100 @@
|
|||||||
|
import os
|
||||||
|
import textwrap
|
||||||
|
from os.path import exists
|
||||||
|
|
||||||
|
from PIL import Image, ImageDraw, ImageFont
|
||||||
|
|
||||||
|
from utils import settings
|
||||||
|
from utils.console import print_step, print_substep
|
||||||
|
from utils.fonts import getheight
|
||||||
|
from utils.thumbnail import create_thumbnail
|
||||||
|
|
||||||
|
|
||||||
|
def create_fancy_thumbnail(image, text, text_color, padding, wrap=35):
|
||||||
|
print_step(f"Creating fancy thumbnail for: {text}")
|
||||||
|
font_title_size = 47
|
||||||
|
font = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), font_title_size)
|
||||||
|
image_width, image_height = image.size
|
||||||
|
lines = textwrap.wrap(text, width=wrap)
|
||||||
|
y = (
|
||||||
|
(image_height / 2)
|
||||||
|
- (((getheight(font, text) + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
|
||||||
|
+ 30
|
||||||
|
)
|
||||||
|
draw = ImageDraw.Draw(image)
|
||||||
|
|
||||||
|
username_font = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), 30)
|
||||||
|
draw.text(
|
||||||
|
(205, 825),
|
||||||
|
settings.config["settings"]["channel_name"],
|
||||||
|
font=username_font,
|
||||||
|
fill=text_color,
|
||||||
|
align="left",
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(lines) == 3:
|
||||||
|
lines = textwrap.wrap(text, width=wrap + 10)
|
||||||
|
font_title_size = 40
|
||||||
|
font = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), font_title_size)
|
||||||
|
y = (
|
||||||
|
(image_height / 2)
|
||||||
|
- (((getheight(font, text) + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
|
||||||
|
+ 35
|
||||||
|
)
|
||||||
|
elif len(lines) == 4:
|
||||||
|
lines = textwrap.wrap(text, width=wrap + 10)
|
||||||
|
font_title_size = 35
|
||||||
|
font = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), font_title_size)
|
||||||
|
y = (
|
||||||
|
(image_height / 2)
|
||||||
|
- (((getheight(font, text) + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
|
||||||
|
+ 40
|
||||||
|
)
|
||||||
|
elif len(lines) > 4:
|
||||||
|
lines = textwrap.wrap(text, width=wrap + 10)
|
||||||
|
font_title_size = 30
|
||||||
|
font = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), font_title_size)
|
||||||
|
y = (
|
||||||
|
(image_height / 2)
|
||||||
|
- (((getheight(font, text) + (len(lines) * padding) / len(lines)) * len(lines)) / 2)
|
||||||
|
+ 30
|
||||||
|
)
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
draw.text((120, y), line, font=font, fill=text_color, align="left")
|
||||||
|
y += getheight(font, line) + padding
|
||||||
|
|
||||||
|
return image
|
||||||
|
|
||||||
|
|
||||||
|
def background_thumbnail(reddit_id, title_thumb, subreddit):
|
||||||
|
if not exists(f"./results/{subreddit}/thumbnails"):
|
||||||
|
print_substep(
|
||||||
|
"The 'results/thumbnails' folder could not be found so it was automatically created."
|
||||||
|
)
|
||||||
|
os.makedirs(f"./results/{subreddit}/thumbnails")
|
||||||
|
# get the first file with the .png extension from assets/backgrounds and use it as a background for the thumbnail
|
||||||
|
first_image = next(
|
||||||
|
(file for file in os.listdir("assets/backgrounds") if file.endswith(".png")),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
if first_image is None:
|
||||||
|
print_substep("No png files found in assets/backgrounds", "red")
|
||||||
|
|
||||||
|
else:
|
||||||
|
font_family = settings.config["settings"]["background"]["background_thumbnail_font_family"]
|
||||||
|
font_size = settings.config["settings"]["background"]["background_thumbnail_font_size"]
|
||||||
|
font_color = settings.config["settings"]["background"]["background_thumbnail_font_color"]
|
||||||
|
thumbnail = Image.open(f"assets/backgrounds/{first_image}")
|
||||||
|
width, height = thumbnail.size
|
||||||
|
thumbnailSave = create_thumbnail(
|
||||||
|
thumbnail,
|
||||||
|
font_family,
|
||||||
|
font_size,
|
||||||
|
font_color,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
title_thumb,
|
||||||
|
)
|
||||||
|
thumbnailSave.save(f"./assets/temp/{reddit_id}/thumbnail.png")
|
||||||
|
print_substep(f"Thumbnail - Building Thumbnail in assets/temp/{reddit_id}/thumbnail.png")
|
Loading…
Reference in new issue