-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththumbnail_generation.py
More file actions
98 lines (78 loc) · 4.09 KB
/
Copy paththumbnail_generation.py
File metadata and controls
98 lines (78 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import numpy as np
import imageio.v2 as imageio
import cv2
from PIL import Image
import random
import os
import subprocess
import platform
def generate_thumbnail_with_text(title, images_directory):
thumbnail_size = (1280, 720)
title + ".jpg"
output_thumbnail_path = os.path.join("output", "thumbnails", title)
thumbnail_text = "" # Your long text
last_input_image_path = None
# Ask for confirmation about the image
text_thumbnail_input = input("\n\033[94mWhat is the text inside the thumbnail?\033[0m\nAnswer: ").strip()
thumbnail_text = text_thumbnail_input
if not os.path.exists(images_directory):
os.makedirs(images_directory)
while True:
if last_input_image_path is None:
# List all files in the images folder
image_files = os.listdir(images_directory)
# Filter out non-image files
image_files = [file for file in image_files if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', ".webp"))]
# Select a random image from the list
input_image_path = os.path.join(images_directory, random.choice(image_files))
else:
input_image_path = last_input_image_path
# Read the input image using imageio
img = imageio.imread(input_image_path)
# Resize the image to the thumbnail size
img = np.array(Image.fromarray(img).resize(thumbnail_size))
# Add text
font_scale = 2 # Increase font scale to make the text larger
font_thickness = 5
# Split text into two lines without splitting words
words = thumbnail_text.split()
mid = len(words) // 2
line1 = ' '.join(words[:mid])
line2 = ' '.join(words[mid:])
# Get bounding box of the text lines
(text_width1, text_height), _ = cv2.getTextSize(line1, cv2.FONT_HERSHEY_DUPLEX, font_scale, font_thickness)
(text_width2, _), _ = cv2.getTextSize(line2, cv2.FONT_HERSHEY_DUPLEX, font_scale, font_thickness)
max_text_width = max(text_width1, text_width2)
background_width = max_text_width + 2 * 10 # Adjusted for padding
background_height = text_height * 2 + 7 * 10 # Adjusted for two lines
# Generate random positions for text and background
background_x = random.randint(0, thumbnail_size[0] - background_width)
background_y = random.randint(0, thumbnail_size[1] - background_height)
# Draw background rectangle
img = cv2.rectangle(img, (background_x, background_y), (background_x + background_width, background_y + background_height), (0, 0, 0), -1)
# Calculate text positions
text_x = background_x + 10
text_y = background_y + 23 + text_height
# Draw text
cv2.putText(img, line1, (text_x, text_y), cv2.FONT_HERSHEY_DUPLEX, font_scale, (255, 255, 255), font_thickness)
cv2.putText(img, line2, (text_x, text_y + text_height + 10), cv2.FONT_HERSHEY_DUPLEX, font_scale, (255, 255, 255), font_thickness)
# Save the thumbnail
imageio.imwrite(output_thumbnail_path, img)
# Open the generated image
if platform.system() == 'Windows':
os.startfile(output_thumbnail_path)
elif platform.system() == 'Darwin': # macOS
subprocess.Popen(['open', output_thumbnail_path])
else: # Assuming Linux or other POSIX-compliant system
subprocess.Popen(['xdg-open', output_thumbnail_path])
# Ask for confirmation about the image
image_confirmation = input("\n\033[94mIs the generated image good? (yes/no): \033[0m\nAnswer: ").strip().lower()
if image_confirmation == 'yes':
last_input_image_path = input_image_path
else:
last_input_image_path = None
continue
# Ask for confirmation about text alignment
text_confirmation = input("\n\033[94mIs the text alignment correct? (yes/no): \033[0m\nAnswer: ").strip().lower()
if text_confirmation == 'yes':
break