File size: 9,879 Bytes
cf65d69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# 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"
        )