# -----------------------------
# Resolve Base Directory (Sandbox Safe)
# -----------------------------
try:
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
except NameError:
BASE_DIR = os.getcwd()
ASSETS_DIR = os.path.join(BASE_DIR, "assets")
OUTPUT_DIR = os.path.join(BASE_DIR, "output")
# -----------------------------
# Configuration
# -----------------------------
MOCKUP_PATH = os.path.join(ASSETS_DIR, "shirt_mockup.png")
DESIGN_PATH = os.path.join(ASSETS_DIR, "design.png")
OUTPUT_PATH = os.path.join(OUTPUT_DIR, "shirt_mockup_result.png")
# Position and size of the design on the shirt
DESIGN_WIDTH_RATIO = 0.45
DESIGN_X_RATIO = 0.275
DESIGN_Y_RATIO = 0.32
DESIGN_OPACITY = 0.95
# -----------------------------
# Asset Creation Helpers
# -----------------------------
def ensure_directories():
os.makedirs(ASSETS_DIR, exist_ok=True)
os.makedirs(OUTPUT_DIR, exist_ok=True)
def create_placeholder_mockup(path):
img = Image.new("RGBA", (1200, 1600), (245, 245, 245, 255))
draw = ImageDraw.Draw(img)
draw.rectangle([300, 400, 900, 1300], outline=(180, 180, 180), width=6)
draw.text((460, 820), "SHIRT MOCKUP", fill=(150, 150, 150))
img.save(path)
def create_placeholder_design(path):
img = Image.new("RGBA", (800, 600), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)
draw.rectangle([100, 100, 700, 500], outline=(0, 0, 0), width=6)
draw.text((260, 260), "DESIGN", fill=(0, 0, 0))
img.save(path)
# -----------------------------
# Ensure Assets Exist (Sandbox Safe)
# -----------------------------
ensure_directories()
if not os.path.isfile(MOCKUP_PATH):
create_placeholder_mockup(MOCKUP_PATH)
if not os.path.isfile(DESIGN_PATH):
create_placeholder_design(DESIGN_PATH)
# -----------------------------
# Load Images
# -----------------------------
mockup = Image.open(MOCKUP_PATH).convert("RGBA")
design = Image.open(DESIGN_PATH).convert("RGBA")
# -----------------------------
# Resize Design
# -----------------------------
shirt_width, shirt_height = mockup.size
new_width = int(shirt_width * DESIGN_WIDTH_RATIO)
new_height = int(new_width * design.height / design.width)
design = design.resize((new_width, new_height), Image.LANCZOS)
# -----------------------------
# Adjust Design Opacity
# -----------------------------
alpha = design.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(DESIGN_OPACITY)
design.putalpha(alpha)
# -----------------------------
# Position Design
# -----------------------------
x_pos = int(shirt_width * DESIGN_X_RATIO)
y_pos = int(shirt_height * DESIGN_Y_RATIO)
# -----------------------------
# Composite Images
# -----------------------------
final_image = mockup.copy()
final_image.paste(design, (x_pos, y_pos), design)
# -----------------------------
# Save Output
# -----------------------------
final_image.save(OUTPUT_PATH, "PNG")
print("Mockup generated successfully")
print(f"Base directory: {BASE_DIR}")
print(f"Output file: {OUTPUT_PATH}")
# -----------------------------
# Runtime Tests (Do Not Remove)
# -----------------------------
def _self_test():
# Directory checks
assert os.path.isdir(ASSETS_DIR), "Assets directory missing"
assert os.path.isdir(OUTPUT_DIR), "Output directory missing"
# File checks
assert os.path.isfile(MOCKUP_PATH), "Mockup image missing"
assert os.path.isfile(DESIGN_PATH), "Design image missing"
assert os.path.isfile(OUTPUT_PATH), "Output image missing"
# Image sanity
with Image.open(OUTPUT_PATH) as img:
assert img.width > 0 and img.height > 0, "Invalid output image"
# Placeholder integrity
with Image.open(MOCKUP_PATH) as m:
assert m.mode == "RGBA", "Mockup image mode invalid"
_self_test()
"""
top of page
bottom of page