finish fitness

This commit is contained in:
Samuel Lazareanu
2023-04-24 21:15:18 +03:00
parent b5de57ef57
commit c82a7f3e2f
176 changed files with 5758 additions and 69 deletions

View File

@@ -38,7 +38,6 @@ def split_string(string, max_chars_per_line):
return lines[0] return lines[0]
def darken_images(images_folder, output_folder): def darken_images(images_folder, output_folder):
# Set desired darkness # Set desired darkness
dark = 0.5 dark = 0.5
@@ -135,3 +134,38 @@ def create_new_topic_dirs(topic, project_dir):
os.makedirs(f"{project_dir}/sources/images/{topic}") os.makedirs(f"{project_dir}/sources/images/{topic}")
os.makedirs(f"{project_dir}/sources/images/{topic}/cropped") os.makedirs(f"{project_dir}/sources/images/{topic}/cropped")
os.makedirs(f"{project_dir}/sources/images/{topic}/cropped/darken") os.makedirs(f"{project_dir}/sources/images/{topic}/cropped/darken")
def fix_text_syntax(font: str, text_file):
with open(text_file, 'r', encoding="utf-8") as file:
lines = file.readlines()
# Bebas can't display -> replace with '
if font.__contains__("Bebas"):
# open file in read mode
file = open(text_file, "r")
replaced_content = ""
# looping through the file
for line in file:
# stripping line break
line = line.strip()
# replacing the texts
new_line = line.replace("", "'").replace("", "'")
# concatenate the new string and add an end-line break
replaced_content = replaced_content + new_line + "\n"
# close the file
file.close()
# Open file in write mode
write_file = open(text_file, "w")
# overwriting the old file contents with the new/replaced content
write_file.write(replaced_content)
# close the file
write_file.close()

23
main.py
View File

@@ -5,8 +5,8 @@ import helper
# Available topics: christian, fitness # Available topics: christian, fitness
TOPIC = "fitness" TOPIC = "fitness"
SHOW_AUTHOR = True SHOW_AUTHOR = True
CUSTOMER_NAME = "test_author" CUSTOMER_NAME = "final_no_logo"
NUM_OF_POSTS = 3 # If number of posts if set to -1, it will so as many posts as in the data file NUM_OF_POSTS = -1 # 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
project_dir = os.getcwd().replace("\\", "/") project_dir = os.getcwd().replace("\\", "/")
@@ -14,8 +14,9 @@ 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"
text_file = f"{project_dir}/sources/text_data/{TOPIC}.txt" text_file = f"{project_dir}/sources/text_data/{TOPIC}.txt"
quote_font = f"{project_dir}/sources/fonts/MouldyCheeseRegular-WyMWG.ttf" # quote_font = f"{project_dir}/sources/fonts/MouldyCheeseRegular-WyMWG.ttf" # Bible
author_font= f"{project_dir}/sources/fonts/MouldyCheeseRegular-WyMWG.ttf" quote_font = f"{project_dir}/sources/fonts/Bebas-KM7y.ttf" # Fitness
author_font = f"{project_dir}/sources/fonts/MangabeyRegular-rgqVO.otf"
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"
@@ -23,16 +24,18 @@ logo_file = f"{project_dir}/sources/logo.png"
if __name__ == "__main__": if __name__ == "__main__":
# helper.create_new_topic_dirs(TOPIC, project_dir) # helper.create_new_topic_dirs(TOPIC, project_dir)
# helper.fix_text_syntax(quote_font, text_file)
# helper.cut_images_new(images_folder, images_folder_cropped) # helper.cut_images_new(images_folder, images_folder_cropped)
# helper.darken_images(images_folder_cropped, images_folder_cropped_darken) # helper.darken_images(images_folder_cropped, images_folder_cropped_darken)
# 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,
quote_font=quote_font, author_font=author_font, output_folder=output_folder, # quote_font=quote_font, author_font=author_font, output_folder=output_folder,
logo_file=logo_file, customer_name=CUSTOMER_NAME, number_of_posts=NUM_OF_POSTS, show_author=SHOW_AUTHOR) # 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,
# font_dir=font_dir, output_folder=output_folder, quote_font=quote_font, author_font=author_font, output_folder=output_folder,
# customer_name=customer_name, number_of_posts=number_of_posts) customer_name=CUSTOMER_NAME, number_of_posts=NUM_OF_POSTS, show_author=SHOW_AUTHOR)

View File

@@ -23,10 +23,6 @@ def create_dirs(output_folder, customer_name):
def create_posts(images_folder, text_file, quote_font, author_font, output_folder, customer_name, number_of_posts, logo_file: str = None, show_author : bool = False): def create_posts(images_folder, text_file, quote_font, author_font, output_folder, customer_name, number_of_posts, logo_file: str = None, show_author : bool = False):
run_time_average = 0
if number_of_posts > 1:
start_time_total = time.time()
# json_data = json_handler.get_data(json_file) # json_data = json_handler.get_data(json_file)
# verses: str = json_data[0] # verses: str = json_data[0]
# refs: str = json_data[1] # refs: str = json_data[1]
@@ -35,10 +31,17 @@ def create_posts(images_folder, text_file, quote_font, author_font, output_folde
with open(text_file, 'r', encoding='utf-8') as file: with open(text_file, 'r', encoding='utf-8') as file:
quotes = file.readlines() quotes = file.readlines()
# If number_of_posts is set to -1, it will do it for the amount of quotes there is in the data file
if number_of_posts == -1:
number_of_posts = len(quotes) - 1
run_time_average = 0
if number_of_posts > 1:
start_time_total = time.time()
# Get list of photos in the specified folder and shuffle it # Get list of photos in the specified folder and shuffle it
image_num = list() image_num = list()
image_files = [f"{images_folder}/{file}" for file in os.listdir(images_folder) if file.endswith(".jpg") or file.endswith(".png")] image_files = [f"{images_folder}/{file}" for file in os.listdir(images_folder) if file.endswith(".jpg") or file.endswith(".png") or file.endswith(".jpeg")]
random_for_image = random.randint(0, len(image_files) - 1) random_for_image = random.randint(0, len(image_files) - 1)
for i in range(number_of_posts): for i in range(number_of_posts):
image_num.append((random_for_image + i) % len(image_files)) image_num.append((random_for_image + i) % len(image_files))
@@ -47,9 +50,6 @@ def create_posts(images_folder, text_file, quote_font, author_font, output_folde
# Creating folder for customer # Creating folder for customer
output_path = create_dirs(output_folder, customer_name) output_path = create_dirs(output_folder, customer_name)
# If number_of_posts is set to -1, it will do it for the amount of quotes there is in the data file
if number_of_posts == -1:
number_of_posts = len(quotes)-1
for i in range(number_of_posts): for i in range(number_of_posts):
start_time = time.time() start_time = time.time()
print(f"Creating Post #{i}") print(f"Creating Post #{i}")
@@ -57,12 +57,11 @@ def create_posts(images_folder, text_file, quote_font, author_font, output_folde
text = quotes[i] text = quotes[i]
quote = text.split(":::") quote = text.split(":::")
quote_text = quote[0] quote_text = quote[0]
if show_author: if show_author and quote_text != text:
author_text = quote[1] author_text = quote[1]
if author_text.rstrip(" ") == "": if author_text.rstrip(" ") == "":
author_text = None 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]
del image_num[0] del image_num[0]
@@ -75,30 +74,31 @@ def create_posts(images_folder, text_file, quote_font, author_font, output_folde
file_name = f"/{i}-{image_license}.jpg" file_name = f"/{i}-{image_license}.jpg"
create_post(image_file=image_file, quote_text=quote_text, create_post(image_file=image_file, quote_text=quote_text,
font_dir=font_dir, output_path=output_path, file_name=file_name, quote_font=quote_font, author_font=author_font, output_path=output_path, file_name=file_name,
logo_file=logo_file, customer_name=customer_name, author_text=author_text) 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
run_time_average += run_time run_time_average += run_time
print(f"\033[0;34m DONE #{i}, Run time:", round(run_time, 2), "seconds! \033[0m", output_path) print(f"\033[0;34m DONE #{i}, Run time:", round(run_time, 2), "seconds! \033[0m", output_path)
author_text = ""
quote_text = ""
if number_of_posts > 1: if number_of_posts > 1:
run_time_average /= number_of_posts run_time_average /= number_of_posts
end_time_total = time.time() end_time_total = time.time()
run_time_total = end_time_total - start_time_total run_time_total = end_time_total - start_time_total
print(f"\n\033[0;32mDone making {number_of_posts} posts for {customer_name}!" print(f"\n\033[0;32mDone making {number_of_posts} posts for {customer_name}!"
f"\nTotal run time:", round(run_time_total, 2), "seconds!" f"\nTotal run time:", round(run_time_total, 2), "seconds = ", round(run_time_total / 60, 2), " minutes!",
f"\nAverage run time:", round(run_time_average, 2), f"\nAverage run time:", round(run_time_average, 2), "seconds!\033[0m")
"seconds = ", round(run_time_average / 60, 2), " minutes! \033[0m")
def create_post(image_file, quote_text, font_dir, output_path, file_name, logo_file, customer_name, author_text: str = None): def create_post(image_file, quote_text, quote_font, author_font, 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)
# Load selected font # Load selected font
font = ImageFont.truetype(font=f'{font_dir}', size=75) quote_font = ImageFont.truetype(font=f'{quote_font}', size=75)
# Create DrawText object # Create DrawText object
draw = ImageDraw.Draw(im=img) draw = ImageDraw.Draw(im=img)
@@ -113,6 +113,8 @@ def create_post(image_file, quote_text, font_dir, output_path, file_name, logo_f
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=quote_text, width=max_char_count) new_text = textwrap.fill(text=quote_text, width=max_char_count)
# FIX FONT WITH SPACES
new_text = new_text.replace(" ", " ")
# 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
@@ -124,28 +126,29 @@ def create_post(image_file, quote_text, font_dir, output_path, file_name, logo_f
# Draw the shadow text # Draw the shadow text
shadow_color = (0, 0, 0, 128) shadow_color = (0, 0, 0, 128)
shadow_position = (x_text+5, y_text+5) shadow_position = (x_text+5, y_text+5)
draw.text(shadow_position, new_text, font=font, fill=shadow_color, anchor='mm', align='center') draw.text(shadow_position, new_text, font=quote_font, fill=shadow_color, anchor='mm', align='center')
# Add main text to the image # Add main text to the image
draw.text(position, text=new_text, font=font, fill=(255, 255, 255, 255), anchor='mm', draw.text(position, text=new_text, font=quote_font, fill=(255, 255, 255, 255), anchor='mm',
align='center') align='center')
if author_text is not None: if author_text is not None:
# Add author text # Add author text
# Count '\n' in the text to see how many lines there are # Count '\n' in the text to see how many lines there are
author_font = ImageFont.truetype(font=f'{author_font}', size=45)
num_of_lines = new_text.count("\n") + 1 num_of_lines = new_text.count("\n") + 1
line_height = 60 # TODO CHECK REAL HEIGHT line_height = 55 # TODO CHECK REAL HEIGHT
text_height = line_height * num_of_lines text_height = line_height * num_of_lines + 40
# TODO CHANGE AUTHORS FONT # TODO CHANGE AUTHORS FONT
author_position = (position[0], position[1] + text_height) 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') draw.text(author_position, text=author_text, font=author_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)
# Reduce the alpha of the overlay image by 50% # Reduce the alpha of the overlay image by 40%
alpha = 0.7 alpha = 0.6
enhancer = ImageEnhance.Brightness(img_logo) enhancer = ImageEnhance.Brightness(img_logo)
img_logo_darken = enhancer.enhance(alpha) img_logo_darken = enhancer.enhance(alpha)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

2
sources/fonts/info.txt Normal file
View File

@@ -0,0 +1,2 @@
license: Freeware
link: https://www.fontspace.com/bebas-font-f13400

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.2 KiB

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

View File

Before

Width:  |  Height:  |  Size: 742 KiB

After

Width:  |  Height:  |  Size: 742 KiB

View File

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.3 MiB

View File

Before

Width:  |  Height:  |  Size: 1.0 MiB

After

Width:  |  Height:  |  Size: 1.0 MiB

View File

Before

Width:  |  Height:  |  Size: 735 KiB

After

Width:  |  Height:  |  Size: 735 KiB

View File

Before

Width:  |  Height:  |  Size: 1.0 MiB

After

Width:  |  Height:  |  Size: 1.0 MiB

View File

Before

Width:  |  Height:  |  Size: 2.4 MiB

After

Width:  |  Height:  |  Size: 2.4 MiB

View File

Before

Width:  |  Height:  |  Size: 288 KiB

After

Width:  |  Height:  |  Size: 288 KiB

View File

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

View File

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

View File

Before

Width:  |  Height:  |  Size: 89 KiB

After

Width:  |  Height:  |  Size: 89 KiB

View File

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View File

Before

Width:  |  Height:  |  Size: 7.5 KiB

After

Width:  |  Height:  |  Size: 7.5 KiB

View File

Before

Width:  |  Height:  |  Size: 4.0 KiB

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 742 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB

Some files were not shown because too many files have changed in this diff Show More