Shirt-Branding / app.py
Engineer786's picture
Update app.py
0a3e400 verified
import streamlit as st
from PIL import Image, ImageEnhance, ImageOps
import io
# Ensure the design has an alpha channel
def ensure_alpha(image):
if image.mode != "RGBA":
image = image.convert("RGBA")
return image
# Function to blend design with shirt
def blend_design_with_shirt(shirt, design, position=(0, 0), size=(200, 200)):
# Ensure both images are in RGBA mode
shirt = ensure_alpha(shirt)
design = ensure_alpha(design)
# Resize the design
design = design.resize(size)
# Create a mask for blending (ensure transparency mask matches dimensions)
mask = design.split()[3] # Extract alpha channel
# Adjust design transparency for blending
blended_alpha = mask.point(lambda p: p * 0.7) # Reduce opacity by 30%
design.putalpha(blended_alpha)
# Create a new layer to blend design
blended_layer = Image.new("RGBA", shirt.size, (0, 0, 0, 0))
blended_layer.paste(design, position, design)
# Blend the design with the shirt
shirt = Image.alpha_composite(shirt, blended_layer)
return shirt
# Function to add shadow effect
def apply_texture_and_shadow(shirt, design, position=(0, 0), size=(200, 200)):
# Ensure both images are in RGBA mode
shirt = ensure_alpha(shirt)
design = ensure_alpha(design)
# Resize design
design = design.resize(size)
# Create shadow
shadow = design.copy()
shadow = shadow.point(lambda p: p * 0) # Create a dark shadow
shadow.putalpha(100) # Adjust shadow opacity
# Paste shadow onto shirt
shirt.paste(shadow, (position[0] + 5, position[1] + 5), shadow) # Offset for shadow
# Paste design on shirt
shirt.paste(design, position, design)
return shirt
# Function to load and display shirt template with transparency
def load_shirt_template():
# Load the shirt template with a transparent background
shirt = Image.open("shirt_template.png") # Ensure this file is in the same directory or update the path
return ensure_alpha(shirt)
# Function to display the app interface
def display_shirt_customizer():
st.title("Shirt Branding Customizer")
# Allow users to upload a design
uploaded_file = st.file_uploader("Upload Your Design", type=["png", "jpg", "jpeg"])
if uploaded_file is not None:
# Load shirt template with transparent background
shirt = load_shirt_template()
# Open user-uploaded design
design = Image.open(uploaded_file)
# Get design size and position from user inputs
design_size = st.slider("Design Size", min_value=50, max_value=300, value=200)
x_pos = st.slider("Position X (horizontal)", min_value=0, max_value=shirt.width - design_size, value=100)
y_pos = st.slider("Position Y (vertical)", min_value=0, max_value=shirt.height - design_size, value=100)
# Choose blending effect
effect = st.selectbox("Choose Effect", ["Blend Only", "Shadow Effect"])
# Apply the design with selected effect
if effect == "Blend Only":
shirt_with_design = blend_design_with_shirt(shirt, design, position=(x_pos, y_pos), size=(design_size, design_size))
else:
shirt_with_design = apply_texture_and_shadow(shirt, design, position=(x_pos, y_pos), size=(design_size, design_size))
# Display the customized shirt
st.image(shirt_with_design, caption="Your Customized Shirt", use_column_width=True)
# Option to download the final design
buf = io.BytesIO()
shirt_with_design.save(buf, format="PNG")
buf.seek(0)
st.download_button(
label="Download Your Design",
data=buf,
file_name="custom_shirt_design.png",
mime="image/png"
)
# Main entry point of the app
if __name__ == "__main__":
display_shirt_customizer()