app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
import streamlit as st from PIL import Image, ImageDraw, ImageFont from io import BytesIO # Title of the app st.title("Image Watermark Editor with Live Preview") # File uploader for image uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"]) # Initialize variables if uploaded_file is not None: # Open and display the uploaded image original_image = Image.open(uploaded_file).convert("RGBA") st.image(original_image, caption="Original Image", use_column_width=True) # Get image dimensions width, height = original_image.size # Input for watermark text watermark_text = st.text_input("Watermark Text", "Your Watermark") # Input for font size font_size = st.slider("Font Size", min_value=10, max_value=100, value=30) # Input for rotation angle rotation = st.slider("Rotation (degrees)", min_value=0, max_value=360, value=0) # Input for position pos_x = st.slider("X Position", min_value=0, max_value=width, value=int(width / 2)) pos_y = st.slider("Y Position", min_value=0, max_value=height, value=int(height / 2)) # Color picker for watermark text color text_color = st.color_picker("Text Color", "#FFFFFF") # Default white color # Button to apply watermark if st.button("Apply Watermark"): # Create a new image for the watermark text_image = Image.new('RGBA', (width, height), (255, 255, 255, 0)) text_draw = ImageDraw.Draw(text_image) # Load a default font or specify a TTF font file path try: font = ImageFont.truetype("arial.ttf", font_size) except IOError: font = ImageFont.load_default() # Draw the watermark text onto the transparent image text_draw.text((pos_x, pos_y), watermark_text, font=font, fill=text_color) # Rotate the text image text_image = text_image.rotate(rotation, expand=1) # Create a new canvas to match the original image size watermark_image = Image.new('RGBA', original_image.size, (255, 255, 255, 0)) # Calculate the position to paste the rotated text image on the canvas text_width, text_height = text_image.size paste_x = (watermark_image.width - text_width) // 2 paste_y = (watermark_image.height - text_height) // 2 # Paste the rotated text image onto the watermark canvas watermark_image.paste(text_image, (paste_x, paste_y), text_image) # Composite the watermark onto the original image image_with_watermark = Image.alpha_composite(original_image, watermark_image) # Convert back to RGB for display image_with_watermark = image_with_watermark.convert("RGB") # Display the image with the watermark st.image(image_with_watermark, caption="Image with Watermark", use_column_width=True) # Create a download button for the watermarked image buffered = BytesIO() image_with_watermark.save(buffered, format="PNG") buffered.seek(0) st.download_button(label="Download Watermarked Image", data=buffered, file_name="watermarked_image.png", mime="image/png") else: st.info("Please upload an image to get started.") |