Spaces:
Sleeping
Sleeping
import streamlit as st | |
from PIL import Image, ImageEnhance, ImageFilter | |
import numpy as np | |
# Helper functions for filters and enhancements | |
def apply_grayscale(image): | |
return image.convert("L") | |
def adjust_brightness(image, factor): | |
enhancer = ImageEnhance.Brightness(image) | |
return enhancer.enhance(factor) | |
def adjust_contrast(image, factor): | |
enhancer = ImageEnhance.Contrast(image) | |
return enhancer.enhance(factor) | |
def adjust_saturation(image, factor): | |
enhancer = ImageEnhance.Color(image) | |
return enhancer.enhance(factor) | |
def apply_blur(image, radius): | |
return image.filter(ImageFilter.GaussianBlur(radius)) | |
def apply_edge_detection(image): | |
return image.filter(ImageFilter.FIND_EDGES) | |
def rotate_image(image, angle): | |
return image.rotate(angle, expand=True) | |
def flip_image(image, direction): | |
if direction == "Horizontal": | |
return image.transpose(Image.FLIP_LEFT_RIGHT) | |
elif direction == "Vertical": | |
return image.transpose(Image.FLIP_TOP_BOTTOM) | |
return image | |
# Streamlit UI | |
st.title("Advanced Image Editor") | |
# Upload image | |
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "png", "jpeg"]) | |
if uploaded_file is not None: | |
# Open image | |
image = Image.open(uploaded_file) | |
# Display original image | |
st.image(image, caption="Original Image", use_column_width=True) | |
# Sidebar for feature selection | |
st.sidebar.title("Image Editing Options") | |
# Basic transformations | |
if st.sidebar.checkbox("Crop"): | |
st.write("Crop feature will be implemented here.") | |
if st.sidebar.checkbox("Resize"): | |
width = st.sidebar.number_input("Width", min_value=10, value=image.width) | |
height = st.sidebar.number_input("Height", min_value=10, value=image.height) | |
image = image.resize((width, height)) | |
if st.sidebar.checkbox("Rotate"): | |
angle = st.sidebar.slider("Angle", 0, 360, 0) | |
image = rotate_image(image, angle) | |
if st.sidebar.checkbox("Flip"): | |
flip_direction = st.sidebar.radio("Direction", ("Horizontal", "Vertical")) | |
image = flip_image(image, flip_direction) | |
# Enhancements | |
if st.sidebar.checkbox("Adjust Brightness"): | |
factor = st.sidebar.slider("Brightness", 0.5, 2.0, 1.0) | |
image = adjust_brightness(image, factor) | |
if st.sidebar.checkbox("Adjust Contrast"): | |
factor = st.sidebar.slider("Contrast", 0.5, 2.0, 1.0) | |
image = adjust_contrast(image, factor) | |
if st.sidebar.checkbox("Adjust Saturation"): | |
factor = st.sidebar.slider("Saturation", 0.5, 2.0, 1.0) | |
image = adjust_saturation(image, factor) | |
# Filters | |
if st.sidebar.checkbox("Grayscale"): | |
image = apply_grayscale(image) | |
if st.sidebar.checkbox("Blur"): | |
radius = st.sidebar.slider("Blur Radius", 1, 10, 2) | |
image = apply_blur(image, radius) | |
if st.sidebar.checkbox("Edge Detection"): | |
image = apply_edge_detection(image) | |
# Display edited image | |
st.image(image, caption="Edited Image", use_column_width=True) | |
# Provide option to download the edited image | |
with st.spinner('Preparing your image for download...'): | |
image.save("edited_image.png") | |
st.download_button( | |
label="Download Edited Image", | |
data=open("edited_image.png", "rb").read(), | |
file_name="edited_image.png", | |
mime="image/png" | |
) | |