Spaces:
Running
Running
import requests | |
from PIL import Image, ImageDraw, ImageFont | |
from io import BytesIO | |
import uuid | |
# --- 1. SETUP: Define your data --- | |
# Input and Output files | |
template_path = 'template_natura_empty.jpg' | |
output_path = f'{uuid.uuid4()}.png' | |
# Image to place on the template | |
# NOTE: Replace this with parameters | |
product_image_url = 'https://production.na01.natura.com/on/demandware.static/-/Sites-natura-br-storefront-catalog/default/dw68595724/NATBRA-89834_1.jpg' | |
product_name = "Homem Cor.agio" | |
original_price = "De: R$ 399,00" | |
final_price = "Por: R$ 167,92" | |
coupon_code = "AGOSTOA" | |
# --- 2. IMAGE PROCESSING --- | |
try: | |
# Load the base template image | |
template_image = Image.open(template_path).convert("RGBA") | |
# Fetch the product image from the URL | |
response = requests.get(product_image_url) | |
product_image_data = BytesIO(response.content) | |
product_image = Image.open(product_image_data).convert("RGBA") | |
# Define the position and size for the product image placeholder | |
# These coordinates were estimated from your template (width, height) | |
box_size = (442, 353) | |
box_position = (140, 280) # (x, y) from top-left corner | |
# --- KEY CHANGE 1: Resize image while preserving aspect ratio --- | |
# The thumbnail method resizes the image to fit within the box_size | |
# without changing its aspect ratio. It modifies the image in-place. | |
product_image_resized = product_image.copy() # Work on a copy | |
product_image_resized.thumbnail(box_size) | |
# --- KEY CHANGE 2: Calculate position to center the image --- | |
# Find the top-left corner to paste the image so it's centered in the box | |
paste_x = box_position[0] + (box_size[0] - product_image_resized.width) // 2 | |
paste_y = box_position[1] + (box_size[1] - product_image_resized.height) // 2 | |
paste_position = (paste_x, paste_y) | |
# Paste the resized product image onto the template | |
template_image.paste(product_image_resized, paste_position, product_image_resized) | |
# --- 3. TEXT DRAWING --- | |
# Create a drawing context | |
draw = ImageDraw.Draw(template_image) | |
# Define fonts. For best results, download a font like 'Montserrat' and provide the path. | |
# Using a default font if a specific one isn't found. | |
try: | |
font_name = ImageFont.truetype("Montserrat-Bold.ttf", 47) | |
font_price_from = ImageFont.truetype("Montserrat-Regular.ttf", 28) | |
font_price = ImageFont.truetype("Montserrat-Bold.ttf", 47) | |
font_cupom = ImageFont.truetype("Montserrat-Bold.ttf", 33) | |
except IOError: | |
print("Arial font not found. Using default font.") | |
font_bold = ImageFont.load_default() | |
font_regular = ImageFont.load_default() | |
font_price = ImageFont.load_default() | |
font_cupom = ImageFont.load_default() | |
# Define text colors | |
white_color = "#FFFFFF" | |
yellow_color = "#FEE161" # A yellow sampled from your design | |
black_color = "#000000" | |
# Add text to the image | |
# The 'anchor="ms"' centers the text horizontally at the given x-coordinate | |
# 1. Product Name | |
draw.text((360, 710), product_name, font=font_name, fill=white_color, anchor="ms") | |
# 2. Original Price | |
draw.text((360, 800), original_price, font=font_price_from, fill=white_color, anchor="ms") | |
# 3. Final Price | |
draw.text((360, 860), final_price, font=font_price, fill=yellow_color, anchor="ms") | |
# 4. Coupon Code | |
draw.text((360, 993), coupon_code, font=font_cupom, fill=black_color, anchor="ms") | |
# --- 4. SAVE THE FINAL IMAGE --- | |
# Save the result as a PNG to preserve quality | |
template_image.save(output_path) | |
print(f"✨ Success! Image saved as '{output_path}'") | |
except FileNotFoundError: | |
print(f"Error: The template file '{template_path}' was not found.") | |
except Exception as e: | |
print(f"An error occurred: {e}") |