Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image, ImageEnhance, ImageFilter
|
3 |
+
import numpy as np
|
4 |
+
import cv2
|
5 |
+
import io
|
6 |
+
|
7 |
+
# Function to adjust brightness, contrast, saturation
|
8 |
+
def adjust_image(img, brightness=1.0, contrast=1.0, saturation=1.0):
|
9 |
+
enhancer = ImageEnhance.Brightness(img)
|
10 |
+
img = enhancer.enhance(brightness)
|
11 |
+
|
12 |
+
enhancer = ImageEnhance.Contrast(img)
|
13 |
+
img = enhancer.enhance(contrast)
|
14 |
+
|
15 |
+
enhancer = ImageEnhance.Color(img)
|
16 |
+
img = enhancer.enhance(saturation)
|
17 |
+
|
18 |
+
return img
|
19 |
+
|
20 |
+
# Function to apply grayscale filter
|
21 |
+
def apply_grayscale(img):
|
22 |
+
return img.convert("L")
|
23 |
+
|
24 |
+
# Function to apply blur effect
|
25 |
+
def apply_blur(img):
|
26 |
+
return img.filter(ImageFilter.GaussianBlur(radius=5))
|
27 |
+
|
28 |
+
# Function to crop image
|
29 |
+
def crop_image(img, left, upper, right, lower):
|
30 |
+
return img.crop((left, upper, right, lower))
|
31 |
+
|
32 |
+
# Function to cut a piece of the image
|
33 |
+
def cut_piece(img, x, y, width, height):
|
34 |
+
img_np = np.array(img)
|
35 |
+
cut_img = img_np[y:y+height, x:x+width]
|
36 |
+
return Image.fromarray(cut_img)
|
37 |
+
|
38 |
+
# Streamlit App UI
|
39 |
+
st.title("Image Editing App")
|
40 |
+
|
41 |
+
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
42 |
+
|
43 |
+
if uploaded_file is not None:
|
44 |
+
# Open the uploaded image
|
45 |
+
img = Image.open(uploaded_file)
|
46 |
+
|
47 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
48 |
+
|
49 |
+
# Adjust brightness, contrast, and saturation
|
50 |
+
st.sidebar.header("Adjust Image")
|
51 |
+
brightness = st.sidebar.slider("Brightness", 0.0, 2.0, 1.0)
|
52 |
+
contrast = st.sidebar.slider("Contrast", 0.0, 2.0, 1.0)
|
53 |
+
saturation = st.sidebar.slider("Saturation", 0.0, 2.0, 1.0)
|
54 |
+
|
55 |
+
img = adjust_image(img, brightness, contrast, saturation)
|
56 |
+
|
57 |
+
# Filters and effects
|
58 |
+
filter_option = st.sidebar.selectbox(
|
59 |
+
"Select a Filter",
|
60 |
+
["None", "Grayscale", "Blur"]
|
61 |
+
)
|
62 |
+
|
63 |
+
if filter_option == "Grayscale":
|
64 |
+
img = apply_grayscale(img)
|
65 |
+
elif filter_option == "Blur":
|
66 |
+
img = apply_blur(img)
|
67 |
+
|
68 |
+
# Crop and cut image functionality
|
69 |
+
crop_option = st.sidebar.checkbox("Crop Image")
|
70 |
+
if crop_option:
|
71 |
+
left = st.sidebar.slider("Left", 0, img.width, 0)
|
72 |
+
upper = st.sidebar.slider("Upper", 0, img.height, 0)
|
73 |
+
right = st.sidebar.slider("Right", left, img.width, img.width)
|
74 |
+
lower = st.sidebar.slider("Lower", upper, img.height, img.height)
|
75 |
+
img = crop_image(img, left, upper, right, lower)
|
76 |
+
|
77 |
+
cut_option = st.sidebar.checkbox("Cut a Piece of Image")
|
78 |
+
if cut_option:
|
79 |
+
x = st.sidebar.slider("X position", 0, img.width, 0)
|
80 |
+
y = st.sidebar.slider("Y position", 0, img.height, 0)
|
81 |
+
width = st.sidebar.slider("Width", 1, img.width - x, 100)
|
82 |
+
height = st.sidebar.slider("Height", 1, img.height - y, 100)
|
83 |
+
img = cut_piece(img, x, y, width, height)
|
84 |
+
|
85 |
+
# Display edited image
|
86 |
+
st.image(img, caption="Edited Image", use_column_width=True)
|
87 |
+
|
88 |
+
# Option to download edited image
|
89 |
+
buffered = io.BytesIO()
|
90 |
+
img.save(buffered, format="PNG")
|
91 |
+
st.download_button(label="Download Image", data=buffered.getvalue(), file_name="edited_image.png", mime="image/png")
|