Spaces:
Running
Running
# import streamlit as st | |
# import cv2 | |
# import numpy as np | |
# from PIL import Image | |
# # Title with emojis | |
# st.title("πΌοΈ Image Processing\n(π Comparison View)") | |
# # Sidebar for image upload and operation selection with emojis | |
# with st.sidebar: | |
# st.write("π€ Upload & Select") | |
# uploaded_file = st.file_uploader("π Upload an Image", type=["png", "jpg", "jpeg"]) | |
# option = st.selectbox("π οΈ Choose a comparison:", [ | |
# "π« None", "β« Convert to Grayscale", "π Rotate Image", "π«οΈ Blur Image", | |
# "π Convert to Color Space", "βοΈ Edge Detection" | |
# ]) | |
# # Function to process the image | |
# def process_image(image, operation, value=None, extra_value=None): | |
# if operation == "π« None": | |
# return image | |
# elif operation == "π² Convert to Grayscale": | |
# return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) | |
# elif operation == "π Rotate Image": | |
# if value is not None: | |
# (h, w) = image.shape[:2] | |
# center = (w // 2, h // 2) | |
# matrix = cv2.getRotationMatrix2D(center, value, 1.0) | |
# return cv2.warpAffine(image, matrix, (w, h)) | |
# elif operation == "π«οΈ Blur Image": | |
# if value is not None: | |
# kernel_size = (value * 2 + 1, value * 2 + 1) # Ensure odd kernel size | |
# return cv2.GaussianBlur(image, kernel_size, 0) | |
# elif operation == "π Convert to Color Space": | |
# if value == "RGB": | |
# return image # Already in RGB | |
# elif value == "BGR2RGB": | |
# return cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Corrected to RGB2BGR | |
# elif value == "Grayscale": | |
# return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) | |
# elif operation == "βοΈ Edge Detection": | |
# if value is not None and extra_value is not None: | |
# gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) | |
# return cv2.Canny(gray_image, value, extra_value) | |
# return image | |
# if uploaded_file is not None: | |
# # Read image | |
# image = Image.open(uploaded_file) | |
# img_array = np.array(image) | |
# processed_img = img_array.copy() | |
# # Operation-specific controls in the main area with emojis | |
# if option == "π Rotate Image": | |
# angle = st.slider("βͺοΈ Select Rotation Angle", -180, 180, 0) | |
# processed_img = process_image(img_array, option, angle) | |
# elif option == "π«οΈ Blur Image": | |
# blur_level = st.slider("π¨ Select Blur Level", 1, 20, 5) | |
# processed_img = process_image(img_array, option, blur_level) | |
# elif option == "π Convert to Color Space": | |
# color_space = st.selectbox("π¨ Choose a color space:", ["RGB", "BGR2RGB", "Grayscale"]) | |
# processed_img = process_image(img_array, option, color_space) | |
# elif option == "βοΈ Edge Detection": | |
# low_threshold = st.slider("π½ Lower Threshold", 0, 255, 50) | |
# high_threshold = st.slider("πΌ Upper Threshold", 0, 255, 150) | |
# processed_img = process_image(img_array, option, low_threshold, high_threshold) | |
# else: | |
# processed_img = process_image(img_array, option) | |
# # Display images side by side with emojis in captions | |
# col1, col2 = st.columns(2) | |
# with col1: | |
# st.image(image, caption="π Original Image", use_container_width=True) | |
# with col2: | |
# # Convert processed image to appropriate format for display | |
# if len(processed_img.shape) == 2: # Grayscale image | |
# processed_img_rgb = cv2.cvtColor(processed_img, cv2.COLOR_GRAY2RGB) | |
# else: | |
# processed_img_rgb = processed_img # Already in RGB | |
# # Dynamic caption with emojis based on operation | |
# if option == "π« None": | |
# caption = "π« No Processing Applied" | |
# elif option == "π² Convert to Grayscale": | |
# caption = "π² Grayscale Image" | |
# elif option == "π Rotate Image": | |
# caption = f"π Rotated by {angle}Β°" | |
# elif option == "π«οΈ Blur Image": | |
# caption = f"π«οΈ Blurred (Level {blur_level})" | |
# elif option == "π Convert to Color Space": | |
# caption = f"π {color_space} Image" | |
# elif option == "βοΈ Edge Detection": | |
# caption = "βοΈ Edge Detection (Canny)" | |
# st.image(processed_img_rgb, caption=caption, use_container_width=True) | |
# # Download button with emoji | |
# if len(processed_img.shape) == 2: | |
# processed_img_download = cv2.cvtColor(processed_img, cv2.COLOR_GRAY2BGR) | |
# else: | |
# processed_img_download = cv2.cvtColor(processed_img, cv2.COLOR_RGB2BGR) | |
# is_success, buffer = cv2.imencode(".png", processed_img_download) | |
# if is_success: | |
# st.download_button( | |
# label="πΎ Download Processed Image", | |
# data=buffer.tobytes(), | |
# file_name="processed_image.png", | |
# mime="image/png" | |
# ) | |
import streamlit as st | |
import cv2 | |
import numpy as np | |
from PIL import Image | |
# Title with emojis | |
st.title("πΌοΈ Image Processing\n(π Comparison View)") | |
# Sidebar for image upload and operation selection with emojis | |
with st.sidebar: | |
st.write("π€ Upload & Select") | |
uploaded_file = st.file_uploader("π Upload an Image", type=["png", "jpg", "jpeg"]) | |
option = st.selectbox("π οΈ Choose a comparison:", [ | |
"π« None", "π² Convert to Grayscale", "π Rotate Image", "π«οΈ Blur Image", | |
"π Convert to Color Space", "βοΈ Edge Detection" | |
]) | |
# Function to process the image | |
def process_image(image, operation, value=None, extra_value=None): | |
if operation == "π« None": | |
return image | |
elif operation == "π² Convert to Grayscale": | |
return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) | |
elif operation == "π Rotate Image": | |
if value is not None: | |
(h, w) = image.shape[:2] | |
center = (w // 2, h // 2) | |
matrix = cv2.getRotationMatrix2D(center, value, 1.0) | |
return cv2.warpAffine(image, matrix, (w, h)) | |
elif operation == "π«οΈ Blur Image": | |
if value is not None: | |
kernel_size = (value * 2 + 1, value * 2 + 1) # Ensure odd kernel size | |
return cv2.GaussianBlur(image, kernel_size, 0) | |
elif operation == "π Convert to Color Space": | |
if value == "RGB": | |
return image # Already in RGB | |
elif value == "BGR2RGB": | |
return cv2.cvtColor(image, cv2.COLOR_RGB2BGR) | |
elif value == "Grayscale": | |
return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) | |
elif operation == "βοΈ Edge Detection": | |
if value is not None and extra_value is not None: | |
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) | |
return cv2.Canny(gray_image, value, extra_value) | |
return image | |
if uploaded_file is not None: | |
# Read image | |
image = Image.open(uploaded_file) | |
img_array = np.array(image) | |
processed_img = img_array.copy() | |
# Operation-specific controls in the main area with emojis | |
if option == "π Rotate Image": | |
angle = st.slider("βͺοΈ Select Rotation Angle", -180, 180, 0) | |
processed_img = process_image(img_array, option, angle) | |
elif option == "π«οΈ Blur Image": | |
blur_level = st.slider("π¨ Select Blur Level", 1, 20, 5) | |
processed_img = process_image(img_array, option, blur_level) | |
elif option == "π Convert to Color Space": | |
color_space = st.selectbox("π¨ Choose a color space:", ["RGB", "BGR2RGB", "Grayscale"]) | |
processed_img = process_image(img_array, option, color_space) | |
elif option == "βοΈ Edge Detection": | |
low_threshold = st.slider("π½ Lower Threshold", 0, 255, 50) | |
high_threshold = st.slider("πΌ Upper Threshold", 0, 255, 150) | |
processed_img = process_image(img_array, option, low_threshold, high_threshold) | |
else: | |
processed_img = process_image(img_array, option) | |
# Display images side by side with emojis in captions | |
col1, col2 = st.columns(2) | |
with col1: | |
st.image(image, caption="π Original Image", use_container_width=True) | |
with col2: | |
# Convert processed image to appropriate format for display | |
if len(processed_img.shape) == 2: # Grayscale image | |
processed_img_rgb = cv2.cvtColor(processed_img, cv2.COLOR_GRAY2RGB) | |
else: | |
processed_img_rgb = processed_img # Already in RGB | |
# Dynamic caption with emojis based on operation | |
caption = "π« No Processing Applied" # Default caption | |
if option == "π« None": | |
caption = "π« No Processing Applied" | |
elif option == "π² Convert to Grayscale": | |
caption = "π² Grayscale Image" | |
elif option == "π Rotate Image": | |
caption = f"π Rotated by {angle}Β°" | |
elif option == "π«οΈ Blur Image": | |
caption = f"π«οΈ Blurred (Level {blur_level})" | |
elif option == "π Convert to Color Space": | |
caption = f"π {color_space} Image" | |
elif option == "βοΈ Edge Detection": | |
caption = "βοΈ Edge Detection (Canny)" | |
st.image(processed_img_rgb, caption=caption, use_container_width=True) | |
# Download button with emoji | |
if len(processed_img.shape) == 2: | |
processed_img_download = cv2.cvtColor(processed_img, cv2.COLOR_GRAY2BGR) | |
else: | |
processed_img_download = cv2.cvtColor(processed_img, cv2.COLOR_RGB2BGR) | |
is_success, buffer = cv2.imencode(".png", processed_img_download) | |
if is_success: | |
st.download_button( | |
label="πΎ Download Processed Image", | |
data=buffer.tobytes(), | |
file_name="processed_image.png", | |
mime="image/png" | |
) |