Image_Opencv / app.py
Kabila22's picture
Add file.txt
cf65d69
# 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"
)