Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image, ImageEnhance, ImageOps, ImageFilter
|
3 |
+
import numpy as np
|
4 |
+
import cv2
|
5 |
+
from skimage import filters, transform
|
6 |
+
|
7 |
+
# App title
|
8 |
+
st.title("Image Editor App")
|
9 |
+
|
10 |
+
# Sidebar
|
11 |
+
st.sidebar.title("Features")
|
12 |
+
uploaded_file = st.sidebar.file_uploader("Upload an Image", type=["png", "jpg", "jpeg"])
|
13 |
+
if uploaded_file:
|
14 |
+
img = Image.open(uploaded_file)
|
15 |
+
img_array = np.array(img)
|
16 |
+
|
17 |
+
# Basic Editing
|
18 |
+
st.sidebar.subheader("Basic Editing")
|
19 |
+
brightness = st.sidebar.slider("Brightness", 0.5, 3.0, 1.0)
|
20 |
+
contrast = st.sidebar.slider("Contrast", 0.5, 3.0, 1.0)
|
21 |
+
saturation = st.sidebar.slider("Saturation", 0.5, 3.0, 1.0)
|
22 |
+
rotation_angle = st.sidebar.slider("Rotate", 0, 360, 0)
|
23 |
+
flip_option = st.sidebar.radio("Flip", ["None", "Horizontal", "Vertical"])
|
24 |
+
|
25 |
+
# Advanced Editing
|
26 |
+
st.sidebar.subheader("Advanced Editing")
|
27 |
+
blur = st.sidebar.slider("Blur", 0, 10, 0)
|
28 |
+
edge_detection = st.sidebar.checkbox("Edge Detection")
|
29 |
+
crop_left = st.sidebar.slider("Crop Left (%)", 0, 50, 0)
|
30 |
+
crop_right = st.sidebar.slider("Crop Right (%)", 0, 50, 0)
|
31 |
+
crop_top = st.sidebar.slider("Crop Top (%)", 0, 50, 0)
|
32 |
+
crop_bottom = st.sidebar.slider("Crop Bottom (%)", 0, 50, 0)
|
33 |
+
|
34 |
+
# Creative Features
|
35 |
+
st.sidebar.subheader("Creative Features")
|
36 |
+
text_overlay = st.sidebar.text_input("Text Overlay", "Enter text")
|
37 |
+
text_position_x = st.sidebar.slider("Text X Position", 0, img.width, 50)
|
38 |
+
text_position_y = st.sidebar.slider("Text Y Position", 0, img.height, 50)
|
39 |
+
|
40 |
+
# Apply transformations
|
41 |
+
enhancer = ImageEnhance.Brightness(img)
|
42 |
+
img = enhancer.enhance(brightness)
|
43 |
+
enhancer = ImageEnhance.Contrast(img)
|
44 |
+
img = enhancer.enhance(contrast)
|
45 |
+
enhancer = ImageEnhance.Color(img)
|
46 |
+
img = enhancer.enhance(saturation)
|
47 |
+
|
48 |
+
if rotation_angle:
|
49 |
+
img = img.rotate(rotation_angle)
|
50 |
+
|
51 |
+
if flip_option == "Horizontal":
|
52 |
+
img = ImageOps.mirror(img)
|
53 |
+
elif flip_option == "Vertical":
|
54 |
+
img = ImageOps.flip(img)
|
55 |
+
|
56 |
+
if blur > 0:
|
57 |
+
img = img.filter(ImageFilter.GaussianBlur(blur))
|
58 |
+
|
59 |
+
if edge_detection:
|
60 |
+
edges = cv2.Canny(np.array(img), 100, 200)
|
61 |
+
img = Image.fromarray(edges)
|
62 |
+
|
63 |
+
if any([crop_left, crop_right, crop_top, crop_bottom]):
|
64 |
+
width, height = img.size
|
65 |
+
img = img.crop((
|
66 |
+
width * crop_left // 100,
|
67 |
+
height * crop_top // 100,
|
68 |
+
width * (100 - crop_right) // 100,
|
69 |
+
height * (100 - crop_bottom) // 100
|
70 |
+
))
|
71 |
+
|
72 |
+
# Text Overlay
|
73 |
+
if text_overlay:
|
74 |
+
img = np.array(img)
|
75 |
+
cv2.putText(img, text_overlay, (text_position_x, text_position_y),
|
76 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
|
77 |
+
img = Image.fromarray(img)
|
78 |
+
|
79 |
+
# Display original and edited images
|
80 |
+
st.image(img_array, caption="Original Image", use_column_width=True)
|
81 |
+
st.image(img, caption="Edited Image", use_column_width=True)
|
82 |
+
|
83 |
+
# Download option
|
84 |
+
st.sidebar.download_button(
|
85 |
+
label="Download Edited Image",
|
86 |
+
data=img.tobytes(),
|
87 |
+
file_name="edited_image.png",
|
88 |
+
mime="image/png"
|
89 |
+
)
|
90 |
+
else:
|
91 |
+
st.info("Please upload an image to begin.")
|