import streamlit as st import cv2 import numpy as np from PIL import Image st.title("OpenCV Image Processing") st.write("This is a simple web app to demonstrate how to use OpenCV with Streamlit.") uploaded_file = st.file_uploader( "Choose an image...", type=["png", "jpg", "jpeg"]) if uploaded_file is not None: st.text("Image uploaded successfully.") st.write("Please select an option from the checkboxes below:") options = { "Show Grayscale": False, "Resize Image": False, "Crop Image": False, "Rotate Image": False, "Flip Image": False, } for option_name in options: options[option_name] = st.checkbox(option_name) st.subheader("Original vs Processed Image") col1, col2 = st.columns(2) if uploaded_file is not None: # Check if a file was actually uploaded image = Image.open(uploaded_file).convert( "RGB") # CRUCIAL: Convert to RGB immediately original_image = np.array(image) processed_image = original_image.copy() with col1: st.image(original_image, caption="Original Image", channels="RGB") # Display in RGB if any(options.values()): if options["Show Grayscale"]: processed_image = cv2.cvtColor( processed_image, cv2.COLOR_RGB2GRAY) if options["Resize Image"]: width = st.slider( "New Width", 100, original_image.shape[1] * 2, original_image.shape[1], step=10) height = st.slider( "New Height", 100, original_image.shape[0] * 2, original_image.shape[0], step=10) processed_image = cv2.resize(processed_image, (width, height)) if len(processed_image.shape) == 2: processed_image = cv2.cvtColor( processed_image, cv2.COLOR_GRAY2RGB) if options["Crop Image"]: x1 = st.slider("X1", 0, original_image.shape[1], 0) y1 = st.slider("Y1", 0, original_image.shape[0], 0) x2 = st.slider( "X2", 0, original_image.shape[1], original_image.shape[1]) y2 = st.slider( "Y2", 0, original_image.shape[0], original_image.shape[0]) processed_image = processed_image[y1:y2, x1:x2] if len(processed_image.shape) == 2: processed_image = cv2.cvtColor( processed_image, cv2.COLOR_GRAY2RGB) if options["Rotate Image"]: angle = st.slider("Rotation Angle", -180, 180, 0) rows, cols = processed_image.shape[:2] M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1) processed_image = cv2.warpAffine( processed_image, M, (cols, rows)) if len(processed_image.shape) == 2: processed_image = cv2.cvtColor( processed_image, cv2.COLOR_GRAY2RGB) if options["Flip Image"]: flip_mode = st.selectbox( "Flip Mode", [("Vertical", 0), ("Horizontal", 1), ("Both", -1)]) processed_image = cv2.flip(processed_image, flip_mode[1]) if len(processed_image.shape) == 2: processed_image = cv2.cvtColor( processed_image, cv2.COLOR_GRAY2RGB) with col2: if len(processed_image.shape) == 3: processed_channels = "RGB" # Display in RGB elif len(processed_image.shape) == 2: processed_channels = "GRAY" else: processed_channels = None if processed_channels: st.image(processed_image, caption="Processed Image", channels=processed_channels) else: st.write("Unexpected image format after processing.") else: with col2: st.write("No processing applied yet.")