I am looking to automate image processing for an online store where a logo must be placed on various product images of clothing. Luckily, the newer product images are well aligned in most cases which eases the automation requirements of this task.
I have the following code that easily pastes the logo on these images based on predefined coordinates. However, I am still looking to eliminate errors where pictures of hoodies and jackets, for example, are aligned differently compared with t-shirts and polos. Any ideas on how I can retrieve the logo on top of the piece of clothing such that I could manually adjust any "errors" in Photoshop or similar? I am also open to other solutions that help achieve the desired outcomes.
from PIL import Image
import os
# Path to the folder containing product images
folder_path = "D:/..."
save_path = "D:/..."
# Path to the logo image
logo_path = "D:/.../x.png"
logo = Image.open(logo_path)
logo_width, logo_height = logo.size
# Iterate through each file in the folder
for filename in os.listdir(folder_path):
# if filename.endswith(".png"): # Assuming all images are PNG, you can modify accordingly
# Load the product image
product_image_path = os.path.join(folder_path, filename)
product_image = Image.open(product_image_path)
# Define where logo is placed on product image
x, y = 618, 245
region = (x, y, x + logo_width, y + logo_height)
# Place logo on product image
product_image.paste(logo, region, logo)
# Save or display the modified image (choose one or both)
# Save the modified image
product_image.save(os.path.join(save_path, f"modified_{filename}"))
# Display the modified image (optional)
# product_image.show()