Spaces:
Sleeping
Sleeping
| 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! ๐ผ๏ธ") |