Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from PIL import Image, ImageEnhance, ImageDraw, ImageFont | |
| import numpy as np | |
| import io | |
| # Function to apply grayscale filter | |
| def apply_grayscale(image): | |
| return image.convert("L") | |
| # Function to apply brightness adjustment | |
| def apply_brightness(image, factor): | |
| enhancer = ImageEnhance.Brightness(image) | |
| return enhancer.enhance(factor) | |
| # Function to add text to the image | |
| def add_text(image, text): | |
| draw = ImageDraw.Draw(image) | |
| font = ImageFont.load_default() | |
| width, height = image.size | |
| text_width, text_height = draw.textsize(text, font=font) | |
| # Position text at the bottom center | |
| x = (width - text_width) / 2 | |
| y = height - text_height - 10 | |
| draw.text((x, y), text, font=font, fill="white") | |
| return image | |
| # Streamlit UI | |
| st.title("Image Editor") | |
| st.write("Upload an image and apply filters or add text to it.") | |
| # Image upload | |
| uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) | |
| if uploaded_image is not None: | |
| # Open the image using PIL | |
| image = Image.open(uploaded_image) | |
| # Show the original image | |
| st.image(image, caption="Original Image", use_column_width=True) | |
| # Apply filters and editing options | |
| st.sidebar.title("Edit Options") | |
| # Grayscale filter | |
| if st.sidebar.checkbox("Apply Grayscale"): | |
| image = apply_grayscale(image) | |
| # Brightness adjustment | |
| brightness_factor = st.sidebar.slider("Adjust Brightness", 0.0, 2.0, 1.0) | |
| image = apply_brightness(image, brightness_factor) | |
| # Add text | |
| text = st.sidebar.text_input("Add Text to Image") | |
| if text: | |
| image = add_text(image, text) | |
| # Show edited image | |
| st.image(image, caption="Edited Image", use_column_width=True) | |
| # Download button | |
| buffered = io.BytesIO() | |
| image.save(buffered, format="PNG") | |
| buffered.seek(0) | |
| st.download_button("Download Edited Image", buffered, file_name="edited_image.png", mime="image/png") | |