import streamlit as st from PIL import Image, ImageDraw, ImageFont import io # Function to overlay text on the background image def create_image_with_name(background_path, name): # Load the background image bg_image = Image.open(background_path).convert("RGBA") draw = ImageDraw.Draw(bg_image) # Set font and size try: font = ImageFont.truetype("arial.ttf", 40) # Use a font file available on your system except: font = ImageFont.load_default() # Get text size text_width, text_height = draw.textsize(name, font=font) # Position text in the center text_position = ((bg_image.width - text_width) // 2, (bg_image.height - text_height) // 2) # Add text to image draw.text(text_position, name, font=font, fill=(255, 255, 255, 255)) # White color text return bg_image # Streamlit app layout st.title("Rose Background with Your Name") st.write("Enter your name to generate a rose background with your name on it.") # Input field for name user_name = st.text_input("Enter your name:") # Button to generate image if st.button("Generate"): if user_name: # Create image with name img = create_image_with_name("rose_background.jpg", user_name) # Save to a buffer buffer = io.BytesIO() img.save(buffer, format="PNG") buffer.seek(0) # Display the image st.image(img, caption=f"Rose Background for {user_name}") # Provide download link st.download_button("Download Image", buffer, file_name=f"{user_name}_rose.png", mime="image/png") else: st.warning("Please enter a name.")