Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,59 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from PIL import Image, ImageEnhance
|
| 3 |
-
import numpy as np
|
| 4 |
-
import io
|
| 5 |
|
| 6 |
# Function to apply grayscale filter
|
| 7 |
-
def
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
# Function to adjust brightness
|
| 11 |
def adjust_brightness(image, factor):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
from PIL import Image, ImageEnhance
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Function to apply grayscale filter
|
| 6 |
+
def grayscale(image):
|
| 7 |
+
gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 8 |
+
return gray_img
|
| 9 |
|
| 10 |
# Function to adjust brightness
|
| 11 |
def adjust_brightness(image, factor):
|
| 12 |
+
pil_img = Image.fromarray(image)
|
| 13 |
+
enhancer = ImageEnhance.Brightness(pil_img)
|
| 14 |
+
enhanced_img = enhancer.enhance(factor)
|
| 15 |
+
return cv2.cvtColor(np.array(enhanced_img), cv2.COLOR_RGB2BGR)
|
| 16 |
+
|
| 17 |
+
# Load image from uploaded file
|
| 18 |
+
def load_image(uploaded_file):
|
| 19 |
+
img = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), cv2.IMREAD_COLOR)
|
| 20 |
+
return img
|
| 21 |
+
|
| 22 |
+
st.title("Image Editor")
|
| 23 |
+
st.subheader("Enhance your images with simple filters!")
|
| 24 |
+
|
| 25 |
+
# Upload image
|
| 26 |
+
uploaded_file = st.file_uploader("Choose an image:", type=["jpg", "jpeg", "png"])
|
| 27 |
+
|
| 28 |
+
if uploaded_file is not None:
|
| 29 |
+
image = load_image(uploaded_file)
|
| 30 |
+
st.image(image, channels="BGR") # Display original image
|
| 31 |
+
|
| 32 |
+
# Filter selection
|
| 33 |
+
filter_options = {"Original": image, "Grayscale": grayscale(image.copy())}
|
| 34 |
+
selected_filter = st.selectbox("Select a filter:", list(filter_options.keys()))
|
| 35 |
+
filtered_image = filter_options[selected_filter]
|
| 36 |
+
|
| 37 |
+
# Brightness adjustment slider (optional)
|
| 38 |
+
brightness_factor = st.slider("Adjust Brightness", 0.5, 2.0, step=0.1, value=1.0)
|
| 39 |
+
adjusted_image = adjust_brightness(filtered_image.copy(), brightness_factor)
|
| 40 |
+
|
| 41 |
+
# Display edited image
|
| 42 |
+
st.image(adjusted_image, channels="BGR")
|
| 43 |
+
|
| 44 |
+
# Download button
|
| 45 |
+
if st.button("Download Edited Image"):
|
| 46 |
+
with open("edited_image.jpg", "wb") as f:
|
| 47 |
+
cv2.imwrite("edited_image.jpg", adjusted_image)
|
| 48 |
+
f.write(open("edited_image.jpg", "rb").read())
|
| 49 |
+
st.success("Image downloaded successfully!")
|
| 50 |
+
|
| 51 |
+
st.sidebar.markdown(
|
| 52 |
+
"""
|
| 53 |
+
**About this App:**
|
| 54 |
+
|
| 55 |
+
This is a simple image editor built with Streamlit and deployed on Hugging Face. It allows you to upload images, apply grayscale filter (and optionally adjust brightness), and download the edited version.
|
| 56 |
+
|
| 57 |
+
**Feel free to explore and enhance your images!**
|
| 58 |
+
"""
|
| 59 |
+
)
|