Spaces:
Sleeping
Sleeping
import streamlit as st | |
from PIL import Image, ImageEnhance, ImageOps, ImageFilter | |
import numpy as np | |
import cv2 | |
from skimage import filters, transform | |
# App title | |
st.title("Image Editor App") | |
# Sidebar | |
st.sidebar.title("Features") | |
uploaded_file = st.sidebar.file_uploader("Upload an Image", type=["png", "jpg", "jpeg"]) | |
if uploaded_file: | |
img = Image.open(uploaded_file) | |
img_array = np.array(img) | |
# Basic Editing | |
st.sidebar.subheader("Basic Editing") | |
brightness = st.sidebar.slider("Brightness", 0.5, 3.0, 1.0) | |
contrast = st.sidebar.slider("Contrast", 0.5, 3.0, 1.0) | |
saturation = st.sidebar.slider("Saturation", 0.5, 3.0, 1.0) | |
rotation_angle = st.sidebar.slider("Rotate", 0, 360, 0) | |
flip_option = st.sidebar.radio("Flip", ["None", "Horizontal", "Vertical"]) | |
# Advanced Editing | |
st.sidebar.subheader("Advanced Editing") | |
blur = st.sidebar.slider("Blur", 0, 10, 0) | |
edge_detection = st.sidebar.checkbox("Edge Detection") | |
crop_left = st.sidebar.slider("Crop Left (%)", 0, 50, 0) | |
crop_right = st.sidebar.slider("Crop Right (%)", 0, 50, 0) | |
crop_top = st.sidebar.slider("Crop Top (%)", 0, 50, 0) | |
crop_bottom = st.sidebar.slider("Crop Bottom (%)", 0, 50, 0) | |
# Creative Features | |
st.sidebar.subheader("Creative Features") | |
text_overlay = st.sidebar.text_input("Text Overlay", "Enter text") | |
text_position_x = st.sidebar.slider("Text X Position", 0, img.width, 50) | |
text_position_y = st.sidebar.slider("Text Y Position", 0, img.height, 50) | |
# Apply transformations | |
enhancer = ImageEnhance.Brightness(img) | |
img = enhancer.enhance(brightness) | |
enhancer = ImageEnhance.Contrast(img) | |
img = enhancer.enhance(contrast) | |
enhancer = ImageEnhance.Color(img) | |
img = enhancer.enhance(saturation) | |
if rotation_angle: | |
img = img.rotate(rotation_angle) | |
if flip_option == "Horizontal": | |
img = ImageOps.mirror(img) | |
elif flip_option == "Vertical": | |
img = ImageOps.flip(img) | |
if blur > 0: | |
img = img.filter(ImageFilter.GaussianBlur(blur)) | |
if edge_detection: | |
edges = cv2.Canny(np.array(img), 100, 200) | |
img = Image.fromarray(edges) | |
if any([crop_left, crop_right, crop_top, crop_bottom]): | |
width, height = img.size | |
img = img.crop(( | |
width * crop_left // 100, | |
height * crop_top // 100, | |
width * (100 - crop_right) // 100, | |
height * (100 - crop_bottom) // 100 | |
)) | |
# Text Overlay | |
if text_overlay: | |
img = np.array(img) | |
cv2.putText(img, text_overlay, (text_position_x, text_position_y), | |
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA) | |
img = Image.fromarray(img) | |
# Display original and edited images | |
st.image(img_array, caption="Original Image", use_column_width=True) | |
st.image(img, caption="Edited Image", use_column_width=True) | |
# Download option | |
st.sidebar.download_button( | |
label="Download Edited Image", | |
data=img.tobytes(), | |
file_name="edited_image.png", | |
mime="image/png" | |
) | |
else: | |
st.info("Please upload an image to begin.") | |