Spaces:
Sleeping
Sleeping
Commit
ยท
52b6f3f
1
Parent(s):
4ae2236
image processing added
Browse files- .gitignore +1 -0
- app.py +87 -0
- requirements.txt +0 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
venv/
|
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Function to apply filters
|
| 7 |
+
def apply_filter(image, filter_name, scale):
|
| 8 |
+
if filter_name == "Grayscale":
|
| 9 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 10 |
+
elif filter_name == "Blur":
|
| 11 |
+
# Ensure kernel size is a positive odd integer
|
| 12 |
+
ksize = max(1, scale)
|
| 13 |
+
ksize = ksize if ksize % 2 == 1 else ksize + 1
|
| 14 |
+
return cv2.GaussianBlur(image, (ksize, ksize), 0)
|
| 15 |
+
elif filter_name == "Sharpen":
|
| 16 |
+
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
| 17 |
+
return cv2.filter2D(image, -1, kernel)
|
| 18 |
+
elif filter_name == "Edge Detection":
|
| 19 |
+
# Ensure scale is within valid range for Canny
|
| 20 |
+
scale = max(1, scale)
|
| 21 |
+
return cv2.Canny(image, scale, scale * 2)
|
| 22 |
+
elif filter_name == "Brightness":
|
| 23 |
+
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
| 24 |
+
hsv[:, :, 2] = np.clip(hsv[:, :, 2] + scale, 0, 255)
|
| 25 |
+
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
| 26 |
+
elif filter_name == "Contrast":
|
| 27 |
+
alpha = scale / 50.0
|
| 28 |
+
return cv2.convertScaleAbs(image, alpha=alpha, beta=0)
|
| 29 |
+
elif filter_name == "Threshold":
|
| 30 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 31 |
+
_, thresh = cv2.threshold(gray, scale, 255, cv2.THRESH_BINARY)
|
| 32 |
+
return thresh
|
| 33 |
+
elif filter_name == "Sepia":
|
| 34 |
+
kernel = np.array([[0.272, 0.534, 0.131],
|
| 35 |
+
[0.349, 0.686, 0.168],
|
| 36 |
+
[0.393, 0.769, 0.189]])
|
| 37 |
+
return cv2.transform(image, kernel)
|
| 38 |
+
else:
|
| 39 |
+
return image
|
| 40 |
+
|
| 41 |
+
# Streamlit app
|
| 42 |
+
st.title("๐จ Image Processing App ๐ผ๏ธ")
|
| 43 |
+
st.write("Upload an image and apply filters to see the magic! โจ")
|
| 44 |
+
|
| 45 |
+
# Upload image
|
| 46 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 47 |
+
|
| 48 |
+
if uploaded_file is not None:
|
| 49 |
+
# Read and display the original image
|
| 50 |
+
image = Image.open(uploaded_file)
|
| 51 |
+
image = np.array(image)
|
| 52 |
+
st.write("### Original Image ๐ผ๏ธ")
|
| 53 |
+
st.image(image, caption="Original Image", use_container_width=True)
|
| 54 |
+
|
| 55 |
+
# Select filter
|
| 56 |
+
filter_name = st.selectbox(
|
| 57 |
+
"Choose a filter ๐๏ธ",
|
| 58 |
+
["Grayscale", "Blur", "Sharpen", "Edge Detection", "Brightness", "Contrast", "Threshold", "Sepia"]
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Add slider for filter scale
|
| 62 |
+
scale = st.slider(f"Adjust {filter_name} intensity โ๏ธ", 1, 100, 50)
|
| 63 |
+
|
| 64 |
+
# Apply filter
|
| 65 |
+
processed_image = apply_filter(image, filter_name, scale)
|
| 66 |
+
|
| 67 |
+
# Display input and output images in the same row
|
| 68 |
+
col1, col2 = st.columns(2)
|
| 69 |
+
with col1:
|
| 70 |
+
st.write("### Original Image ๐ผ๏ธ")
|
| 71 |
+
st.image(image, caption="Original Image", use_container_width=True)
|
| 72 |
+
with col2:
|
| 73 |
+
st.write("### Processed Image ๐จ")
|
| 74 |
+
st.image(processed_image, caption=f"{filter_name} Applied", use_container_width=True)
|
| 75 |
+
|
| 76 |
+
# Download button for processed image
|
| 77 |
+
processed_image_pil = Image.fromarray(processed_image)
|
| 78 |
+
st.download_button(
|
| 79 |
+
label="Download Processed Image โฌ๏ธ",
|
| 80 |
+
data=processed_image_pil.tobytes(),
|
| 81 |
+
file_name="processed_image.png",
|
| 82 |
+
mime="image/png"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
st.success("โ
Image processing complete! Enjoy your masterpiece! ๐จ")
|
| 86 |
+
else:
|
| 87 |
+
st.info("๐ Please upload an image to get started! ๐ผ๏ธ")
|
requirements.txt
ADDED
|
Binary file (1.55 kB). View file
|
|
|