Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from PIL import Image, ImageEnhance, ImageFilter, ImageOps | |
| import numpy as np | |
| import io | |
| # App Title | |
| st.title("Advanced Image Editor") | |
| # Sidebar for Upload | |
| uploaded_image = st.sidebar.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"]) | |
| if uploaded_image: | |
| # Load and Display the Image | |
| image = Image.open(uploaded_image) | |
| st.image(image, caption="Uploaded Image", use_column_width=True) | |
| # --- Filters --- | |
| st.header("Filters") | |
| col1, col2 = st.columns(2) | |
| # Grayscale | |
| if col1.button("Grayscale"): | |
| grayscale_image = image.convert("L") | |
| st.image(grayscale_image, caption="Grayscale Image", use_column_width=True) | |
| # Sepia | |
| if col2.button("Sepia"): | |
| sepia_image = ImageOps.colorize(image.convert("L"), "#704214", "#C0A080") | |
| st.image(sepia_image, caption="Sepia Image", use_column_width=True) | |
| # Blur | |
| blur_radius = st.slider("Blur Radius", 0.0, 10.0, 1.0) | |
| if st.button("Apply Blur"): | |
| blurred_image = image.filter(ImageFilter.GaussianBlur(blur_radius)) | |
| st.image(blurred_image, caption="Blurred Image", use_column_width=True) | |
| # --- Adjustments --- | |
| st.header("Adjustments") | |
| # Brightness | |
| brightness = st.slider("Brightness", 0.5, 2.0, 1.0) | |
| enhanced_image = ImageEnhance.Brightness(image).enhance(brightness) | |
| st.image(enhanced_image, caption="Brightness Adjusted", use_column_width=True) | |
| # Contrast | |
| contrast = st.slider("Contrast", 0.5, 2.0, 1.0) | |
| contrast_image = ImageEnhance.Contrast(image).enhance(contrast) | |
| st.image(contrast_image, caption="Contrast Adjusted", use_column_width=True) | |
| # Saturation | |
| saturation = st.slider("Saturation", 0.5, 2.0, 1.0) | |
| saturation_image = ImageEnhance.Color(image).enhance(saturation) | |
| st.image(saturation_image, caption="Saturation Adjusted", use_column_width=True) | |
| # --- Transformations --- | |
| st.header("Transformations") | |
| transform_choice = st.selectbox( | |
| "Select a Transformation", | |
| ["None", "Flip Horizontally", "Flip Vertically", "Rotate 90°", "Rotate 180°"] | |
| ) | |
| if transform_choice == "Flip Horizontally": | |
| transformed_image = image.transpose(Image.FLIP_LEFT_RIGHT) | |
| elif transform_choice == "Flip Vertically": | |
| transformed_image = image.transpose(Image.FLIP_TOP_BOTTOM) | |
| elif transform_choice == "Rotate 90°": | |
| transformed_image = image.rotate(90, expand=True) | |
| elif transform_choice == "Rotate 180°": | |
| transformed_image = image.rotate(180, expand=True) | |
| else: | |
| transformed_image = image | |
| st.image(transformed_image, caption="Transformed Image", use_column_width=True) | |
| # --- Download Edited Image --- | |
| st.header("Download Edited Image") | |
| buf = io.BytesIO() | |
| transformed_image.save(buf, format="PNG") | |
| byte_data = buf.getvalue() | |
| st.download_button( | |
| label="Download Image", | |
| data=byte_data, | |
| file_name="edited_image.png", | |
| mime="image/png" | |
| ) | |
| else: | |
| st.write("Upload an image to get started.") | |