new cut_images. old is kept for christian if nneded

This commit is contained in:
Samuel Lazareanu
2023-04-17 22:55:06 +03:00
parent 82cabcca2b
commit 211af969be
2 changed files with 49 additions and 7 deletions

View File

@@ -60,16 +60,16 @@ def darken_images(images_folder, output_folder):
dark_img.save(f"{output_folder}/{filename}") dark_img.save(f"{output_folder}/{filename}")
def cut_images(images_folder, output_folder): def cut_images_old(images_folder, output_folder):
# Set the target size # Set the target size
target_size = (1080, 1350) target_size = (1080, 1350)
# Loop through all the images in the directory # Loop through all the images in the directory
for filename in os.listdir(images_folder): for filename in os.listdir(images_folder):
if filename.endswith(".jpg") or filename.endswith(".png"): if filename.endswith(".jpg") or filename.endswith(".png"):
# Open the image # Open the image
filepath = os.path.join(images_folder, filename) filepath = os.path.join(images_folder, filename)
img = Image.open(filepath) img = Image.open(filepath)
resize_ration = min(target_size[0] / img.width, target_size[1] / img.height)
# Get the size of the image # Get the size of the image
width, height = img.size width, height = img.size
@@ -87,6 +87,45 @@ def cut_images(images_folder, output_folder):
img.save(f"{output_folder}/{filename}") img.save(f"{output_folder}/{filename}")
def cut_images(images_folder, output_folder):
# Set desired ratio
desired_ratio = 1080 / 1350
# Loop through all files in input folder
for filename in os.listdir(images_folder):
if filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.png'):
# Open the image
img = Image.open(os.path.join(images_folder, filename))
# Get image dimensions
width, height = img.size
ratio = width / height
# Calculate new dimensions
if ratio > desired_ratio:
# Image is wider than desired ratio, crop width
new_width = round(height * desired_ratio)
new_height = height
else:
# Image is taller than desired ratio, crop height
new_width = width
new_height = round(width / desired_ratio)
# Crop the image in the center
left = (width - new_width) / 2
top = (height - new_height) / 2
right = left + new_width
bottom = top + new_height
img = img.crop((left, top, right, bottom))
# Resize the image if necessary
if img.size != (1080, 1350):
img = img.resize((1080, 1350))
# Save the image to output folder
img.save(os.path.join(output_folder, filename))
def create_new_topic_dirs(topic, project_dir): def create_new_topic_dirs(topic, project_dir):
# /customers/___ # /customers/___
if not os.path.exists(f"{project_dir}/customers/{topic}"): if not os.path.exists(f"{project_dir}/customers/{topic}"):
@@ -94,3 +133,5 @@ def create_new_topic_dirs(topic, project_dir):
# /sources/images/___ # /sources/images/___
if not os.path.exists(f"{project_dir}/sources/images/{topic}"): if not os.path.exists(f"{project_dir}/sources/images/{topic}"):
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/darken")

11
main.py
View File

@@ -5,7 +5,7 @@ import helper
project_dir = os.getcwd().replace("\\", "/") project_dir = os.getcwd().replace("\\", "/")
# Available topics: christian, fitness # Available topics: christian, fitness
TOPIC = "christian" TOPIC = "fitness"
# Define the paths and values to everything # Define the paths and values to everything
number_of_posts = 119 number_of_posts = 119
@@ -21,13 +21,14 @@ customer_name = "no_logo"
if __name__ == "__main__": if __name__ == "__main__":
# helper.create_new_topic_dirs(TOPIC, project_dir) # helper.create_new_topic_dirs(TOPIC, project_dir)
# helper.cut_images(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,
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=number_of_posts)
# 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,