Spaces:
Sleeping
Sleeping
File size: 3,268 Bytes
52b6f3f | 1 2 3 4 5 6 7 8 9 10 11 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | import streamlit as st
import cv2
import numpy as np
from PIL import Image
# Function to apply filters
def apply_filter(image, filter_name, scale):
if filter_name == "Grayscale":
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
elif filter_name == "Blur":
# Ensure kernel size is a positive odd integer
ksize = max(1, scale)
ksize = ksize if ksize % 2 == 1 else ksize + 1
return cv2.GaussianBlur(image, (ksize, ksize), 0)
elif filter_name == "Sharpen":
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
return cv2.filter2D(image, -1, kernel)
elif filter_name == "Edge Detection":
# Ensure scale is within valid range for Canny
scale = max(1, scale)
return cv2.Canny(image, scale, scale * 2)
elif filter_name == "Brightness":
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hsv[:, :, 2] = np.clip(hsv[:, :, 2] + scale, 0, 255)
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
elif filter_name == "Contrast":
alpha = scale / 50.0
return cv2.convertScaleAbs(image, alpha=alpha, beta=0)
elif filter_name == "Threshold":
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, scale, 255, cv2.THRESH_BINARY)
return thresh
elif filter_name == "Sepia":
kernel = np.array([[0.272, 0.534, 0.131],
[0.349, 0.686, 0.168],
[0.393, 0.769, 0.189]])
return cv2.transform(image, kernel)
else:
return image
# Streamlit app
st.title("🎨 Image Processing App 🖼️")
st.write("Upload an image and apply filters to see the magic! ✨")
# Upload image
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Read and display the original image
image = Image.open(uploaded_file)
image = np.array(image)
st.write("### Original Image 🖼️")
st.image(image, caption="Original Image", use_container_width=True)
# Select filter
filter_name = st.selectbox(
"Choose a filter 🎛️",
["Grayscale", "Blur", "Sharpen", "Edge Detection", "Brightness", "Contrast", "Threshold", "Sepia"]
)
# Add slider for filter scale
scale = st.slider(f"Adjust {filter_name} intensity ⚙️", 1, 100, 50)
# Apply filter
processed_image = apply_filter(image, filter_name, scale)
# Display input and output images in the same row
col1, col2 = st.columns(2)
with col1:
st.write("### Original Image 🖼️")
st.image(image, caption="Original Image", use_container_width=True)
with col2:
st.write("### Processed Image 🎨")
st.image(processed_image, caption=f"{filter_name} Applied", use_container_width=True)
# Download button for processed image
processed_image_pil = Image.fromarray(processed_image)
st.download_button(
label="Download Processed Image ⬇️",
data=processed_image_pil.tobytes(),
file_name="processed_image.png",
mime="image/png"
)
st.success("✅ Image processing complete! Enjoy your masterpiece! 🎨")
else:
st.info("👆 Please upload an image to get started! 🖼️") |