File size: 4,137 Bytes
95bbbb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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.")