mirror of
https://github.com/aljazceru/Image-Quote-Generator.git
synced 2025-12-19 23:14:21 +01:00
add author display - NEED TO FIND GOOD TEXT
This commit is contained in:
13
main.py
13
main.py
@@ -2,14 +2,14 @@ import os
|
|||||||
import post_handler
|
import post_handler
|
||||||
import helper
|
import helper
|
||||||
|
|
||||||
|
|
||||||
project_dir = os.getcwd().replace("\\", "/")
|
|
||||||
# Available topics: christian, fitness
|
# Available topics: christian, fitness
|
||||||
TOPIC = "christian"
|
TOPIC = "fitness"
|
||||||
|
SHOW_AUTHOR = True
|
||||||
|
CUSTOMER_NAME = "test_author"
|
||||||
|
NUM_OF_POSTS = 3 # If number of posts if set to -1, it will so as many posts as in the data file
|
||||||
|
|
||||||
# Define the paths and values to everything
|
# Define the paths and values to everything
|
||||||
# If number of posts if set to -1, it will so as many posts as in the data file
|
project_dir = os.getcwd().replace("\\", "/")
|
||||||
number_of_posts = 81
|
|
||||||
images_folder = f"{project_dir}/sources/images/{TOPIC}"
|
images_folder = f"{project_dir}/sources/images/{TOPIC}"
|
||||||
images_folder_cropped = f"{images_folder}/cropped"
|
images_folder_cropped = f"{images_folder}/cropped"
|
||||||
images_folder_cropped_darken = f"{images_folder_cropped}/darken"
|
images_folder_cropped_darken = f"{images_folder_cropped}/darken"
|
||||||
@@ -17,7 +17,6 @@ text_file = f"{project_dir}/sources/text_data/{TOPIC}.txt"
|
|||||||
font_dir = "C:/Users/samla/AppData/Local/Microsoft/Windows/Fonts/MouldyCheeseRegular-WyMWG.ttf"
|
font_dir = "C:/Users/samla/AppData/Local/Microsoft/Windows/Fonts/MouldyCheeseRegular-WyMWG.ttf"
|
||||||
output_folder = f"{project_dir}/customers/{TOPIC}"
|
output_folder = f"{project_dir}/customers/{TOPIC}"
|
||||||
logo_file = f"{project_dir}/sources/logo.png"
|
logo_file = f"{project_dir}/sources/logo.png"
|
||||||
customer_name = "everythingod"
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@@ -29,7 +28,7 @@ if __name__ == "__main__":
|
|||||||
# LOGO
|
# LOGO
|
||||||
post_handler.create_posts(images_folder=images_folder_cropped_darken, text_file=text_file,
|
post_handler.create_posts(images_folder=images_folder_cropped_darken, text_file=text_file,
|
||||||
font_dir=font_dir, output_folder=output_folder,
|
font_dir=font_dir, output_folder=output_folder,
|
||||||
logo_file=logo_file, customer_name=customer_name, number_of_posts=number_of_posts)
|
logo_file=logo_file, customer_name=CUSTOMER_NAME, number_of_posts=NUM_OF_POSTS, show_author=SHOW_AUTHOR)
|
||||||
|
|
||||||
# NO LOGO
|
# NO LOGO
|
||||||
# post_handler.create_posts(images_folder=images_folder_cropped_darken, text_file=text_file,
|
# post_handler.create_posts(images_folder=images_folder_cropped_darken, text_file=text_file,
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ def create_dirs(output_folder, customer_name):
|
|||||||
return output_path
|
return output_path
|
||||||
|
|
||||||
|
|
||||||
def create_posts(images_folder, text_file, font_dir, output_folder, customer_name, number_of_posts, logo_file: str = None):
|
def create_posts(images_folder, text_file, font_dir, output_folder, customer_name, number_of_posts, logo_file: str = None, show_author : bool = False):
|
||||||
run_time_average = 0
|
run_time_average = 0
|
||||||
if number_of_posts > 1:
|
if number_of_posts > 1:
|
||||||
start_time_total = time.time()
|
start_time_total = time.time()
|
||||||
@@ -55,6 +55,13 @@ def create_posts(images_folder, text_file, font_dir, output_folder, customer_nam
|
|||||||
print(f"Creating Post #{i}")
|
print(f"Creating Post #{i}")
|
||||||
|
|
||||||
text = quotes[i]
|
text = quotes[i]
|
||||||
|
quote = text.split(":::")
|
||||||
|
quote_text = quote[0]
|
||||||
|
if show_author:
|
||||||
|
author_text = quote[1]
|
||||||
|
if author_text.rstrip(" ") == "":
|
||||||
|
author_text = None
|
||||||
|
|
||||||
|
|
||||||
# Choose a random image file from the list
|
# Choose a random image file from the list
|
||||||
random_image_num = image_num[0]
|
random_image_num = image_num[0]
|
||||||
@@ -67,9 +74,9 @@ def create_posts(images_folder, text_file, font_dir, output_folder, customer_nam
|
|||||||
image_license = image_license[len(image_license)-1].rstrip(".jpg")
|
image_license = image_license[len(image_license)-1].rstrip(".jpg")
|
||||||
|
|
||||||
file_name = f"/{i}-{image_license}.jpg"
|
file_name = f"/{i}-{image_license}.jpg"
|
||||||
create_post(image_file=image_file, text=text,
|
create_post(image_file=image_file, quote_text=quote_text,
|
||||||
font_dir=font_dir, output_path=output_path, file_name=file_name,
|
font_dir=font_dir, output_path=output_path, file_name=file_name,
|
||||||
logo_file=logo_file, customer_name=customer_name)
|
logo_file=logo_file, customer_name=customer_name, author_text=author_text)
|
||||||
|
|
||||||
end_time = time.time()
|
end_time = time.time()
|
||||||
run_time = end_time - start_time
|
run_time = end_time - start_time
|
||||||
@@ -86,7 +93,7 @@ def create_posts(images_folder, text_file, font_dir, output_folder, customer_nam
|
|||||||
"seconds = ", round(run_time_average / 60, 2), " minutes! \033[0m")
|
"seconds = ", round(run_time_average / 60, 2), " minutes! \033[0m")
|
||||||
|
|
||||||
|
|
||||||
def create_post(image_file, text, font_dir, output_path, file_name, logo_file, customer_name):
|
def create_post(image_file, quote_text, font_dir, output_path, file_name, logo_file, customer_name, author_text: str = None):
|
||||||
# Open specific image
|
# Open specific image
|
||||||
img = Image.open(image_file)
|
img = Image.open(image_file)
|
||||||
|
|
||||||
@@ -105,7 +112,7 @@ def create_post(image_file, text, font_dir, output_path, file_name, logo_file, c
|
|||||||
# max_char_count = int(img.size[0] / avg_char_width)
|
# max_char_count = int(img.size[0] / avg_char_width)
|
||||||
max_char_count = 25
|
max_char_count = 25
|
||||||
# Create a wrapped text object using scaled character count
|
# Create a wrapped text object using scaled character count
|
||||||
new_text = textwrap.fill(text=text, width=max_char_count)
|
new_text = textwrap.fill(text=quote_text, width=max_char_count)
|
||||||
# new_text = helper_images.split_string(text, max_char_count)
|
# new_text = helper_images.split_string(text, max_char_count)
|
||||||
# Define the positions of logo and text
|
# Define the positions of logo and text
|
||||||
x_logo = 0
|
x_logo = 0
|
||||||
@@ -123,6 +130,16 @@ def create_post(image_file, text, font_dir, output_path, file_name, logo_file, c
|
|||||||
draw.text(position, text=new_text, font=font, fill=(255, 255, 255, 255), anchor='mm',
|
draw.text(position, text=new_text, font=font, fill=(255, 255, 255, 255), anchor='mm',
|
||||||
align='center')
|
align='center')
|
||||||
|
|
||||||
|
if author_text is not None:
|
||||||
|
# Add author text
|
||||||
|
# Count '\n' in the text to see how many lines there are
|
||||||
|
num_of_lines = new_text.count("\n") + 1
|
||||||
|
line_height = 60 # TODO CHECK REAL HEIGHT
|
||||||
|
text_height = line_height * num_of_lines
|
||||||
|
# TODO CHANGE AUTHORS FONT
|
||||||
|
author_position = (position[0], position[1] + text_height)
|
||||||
|
draw.text(author_position, text=author_text, font=font, fill=(255, 255, 255, 255), anchor='mm', align='center')
|
||||||
|
|
||||||
if logo_file is not None:
|
if logo_file is not None:
|
||||||
# Open logo file
|
# Open logo file
|
||||||
img_logo = Image.open(logo_file)
|
img_logo = Image.open(logo_file)
|
||||||
@@ -144,7 +161,7 @@ def create_post(image_file, text, font_dir, output_path, file_name, logo_file, c
|
|||||||
# Convert from RGBA to RGB
|
# Convert from RGBA to RGB
|
||||||
img_with_logo_rgb = img_with_logo.convert("RGB")
|
img_with_logo_rgb = img_with_logo.convert("RGB")
|
||||||
|
|
||||||
file_name
|
# file_name
|
||||||
|
|
||||||
# Save the image
|
# Save the image
|
||||||
img_with_logo_rgb.save(f"{output_path}/{file_name}")
|
img_with_logo_rgb.save(f"{output_path}/{file_name}")
|
||||||
@@ -156,3 +173,4 @@ def create_post(image_file, text, font_dir, output_path, file_name, logo_file, c
|
|||||||
img.save(f"{output_path}/{file_name}")
|
img.save(f"{output_path}/{file_name}")
|
||||||
# combined.show()
|
# combined.show()
|
||||||
return f"{output_path}/{file_name}"
|
return f"{output_path}/{file_name}"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user