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2
|
3 |
+
# import numpy as np
|
4 |
+
# from PIL import Image
|
5 |
+
|
6 |
+
# # Title with emojis
|
7 |
+
# st.title("πΌοΈ Image Processing\n(π Comparison View)")
|
8 |
+
|
9 |
+
# # Sidebar for image upload and operation selection with emojis
|
10 |
+
# with st.sidebar:
|
11 |
+
# st.write("π€ Upload & Select")
|
12 |
+
# uploaded_file = st.file_uploader("π Upload an Image", type=["png", "jpg", "jpeg"])
|
13 |
+
# option = st.selectbox("π οΈ Choose a comparison:", [
|
14 |
+
# "π« None", "β« Convert to Grayscale", "π Rotate Image", "π«οΈ Blur Image",
|
15 |
+
# "π Convert to Color Space", "βοΈ Edge Detection"
|
16 |
+
# ])
|
17 |
+
|
18 |
+
# # Function to process the image
|
19 |
+
# def process_image(image, operation, value=None, extra_value=None):
|
20 |
+
# if operation == "π« None":
|
21 |
+
# return image
|
22 |
+
# elif operation == "π² Convert to Grayscale":
|
23 |
+
# return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
24 |
+
# elif operation == "π Rotate Image":
|
25 |
+
# if value is not None:
|
26 |
+
# (h, w) = image.shape[:2]
|
27 |
+
# center = (w // 2, h // 2)
|
28 |
+
# matrix = cv2.getRotationMatrix2D(center, value, 1.0)
|
29 |
+
# return cv2.warpAffine(image, matrix, (w, h))
|
30 |
+
# elif operation == "π«οΈ Blur Image":
|
31 |
+
# if value is not None:
|
32 |
+
# kernel_size = (value * 2 + 1, value * 2 + 1) # Ensure odd kernel size
|
33 |
+
# return cv2.GaussianBlur(image, kernel_size, 0)
|
34 |
+
# elif operation == "π Convert to Color Space":
|
35 |
+
# if value == "RGB":
|
36 |
+
# return image # Already in RGB
|
37 |
+
# elif value == "BGR2RGB":
|
38 |
+
# return cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Corrected to RGB2BGR
|
39 |
+
# elif value == "Grayscale":
|
40 |
+
# return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
41 |
+
# elif operation == "βοΈ Edge Detection":
|
42 |
+
# if value is not None and extra_value is not None:
|
43 |
+
# gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
44 |
+
# return cv2.Canny(gray_image, value, extra_value)
|
45 |
+
# return image
|
46 |
+
|
47 |
+
# if uploaded_file is not None:
|
48 |
+
# # Read image
|
49 |
+
# image = Image.open(uploaded_file)
|
50 |
+
# img_array = np.array(image)
|
51 |
+
|
52 |
+
# processed_img = img_array.copy()
|
53 |
+
|
54 |
+
# # Operation-specific controls in the main area with emojis
|
55 |
+
# if option == "π Rotate Image":
|
56 |
+
# angle = st.slider("βͺοΈ Select Rotation Angle", -180, 180, 0)
|
57 |
+
# processed_img = process_image(img_array, option, angle)
|
58 |
+
|
59 |
+
# elif option == "π«οΈ Blur Image":
|
60 |
+
# blur_level = st.slider("π¨ Select Blur Level", 1, 20, 5)
|
61 |
+
# processed_img = process_image(img_array, option, blur_level)
|
62 |
+
|
63 |
+
# elif option == "π Convert to Color Space":
|
64 |
+
# color_space = st.selectbox("π¨ Choose a color space:", ["RGB", "BGR2RGB", "Grayscale"])
|
65 |
+
# processed_img = process_image(img_array, option, color_space)
|
66 |
+
|
67 |
+
# elif option == "βοΈ Edge Detection":
|
68 |
+
# low_threshold = st.slider("π½ Lower Threshold", 0, 255, 50)
|
69 |
+
# high_threshold = st.slider("πΌ Upper Threshold", 0, 255, 150)
|
70 |
+
# processed_img = process_image(img_array, option, low_threshold, high_threshold)
|
71 |
+
|
72 |
+
# else:
|
73 |
+
# processed_img = process_image(img_array, option)
|
74 |
+
|
75 |
+
# # Display images side by side with emojis in captions
|
76 |
+
# col1, col2 = st.columns(2)
|
77 |
+
# with col1:
|
78 |
+
# st.image(image, caption="π Original Image", use_container_width=True)
|
79 |
+
|
80 |
+
# with col2:
|
81 |
+
# # Convert processed image to appropriate format for display
|
82 |
+
# if len(processed_img.shape) == 2: # Grayscale image
|
83 |
+
# processed_img_rgb = cv2.cvtColor(processed_img, cv2.COLOR_GRAY2RGB)
|
84 |
+
# else:
|
85 |
+
# processed_img_rgb = processed_img # Already in RGB
|
86 |
+
|
87 |
+
# # Dynamic caption with emojis based on operation
|
88 |
+
# if option == "π« None":
|
89 |
+
# caption = "π« No Processing Applied"
|
90 |
+
# elif option == "π² Convert to Grayscale":
|
91 |
+
# caption = "π² Grayscale Image"
|
92 |
+
# elif option == "π Rotate Image":
|
93 |
+
# caption = f"π Rotated by {angle}Β°"
|
94 |
+
# elif option == "π«οΈ Blur Image":
|
95 |
+
# caption = f"π«οΈ Blurred (Level {blur_level})"
|
96 |
+
# elif option == "π Convert to Color Space":
|
97 |
+
# caption = f"π {color_space} Image"
|
98 |
+
# elif option == "βοΈ Edge Detection":
|
99 |
+
# caption = "βοΈ Edge Detection (Canny)"
|
100 |
+
|
101 |
+
# st.image(processed_img_rgb, caption=caption, use_container_width=True)
|
102 |
+
|
103 |
+
# # Download button with emoji
|
104 |
+
# if len(processed_img.shape) == 2:
|
105 |
+
# processed_img_download = cv2.cvtColor(processed_img, cv2.COLOR_GRAY2BGR)
|
106 |
+
# else:
|
107 |
+
# processed_img_download = cv2.cvtColor(processed_img, cv2.COLOR_RGB2BGR)
|
108 |
+
|
109 |
+
# is_success, buffer = cv2.imencode(".png", processed_img_download)
|
110 |
+
# if is_success:
|
111 |
+
# st.download_button(
|
112 |
+
# label="πΎ Download Processed Image",
|
113 |
+
# data=buffer.tobytes(),
|
114 |
+
# file_name="processed_image.png",
|
115 |
+
# mime="image/png"
|
116 |
+
# )
|
117 |
+
|
118 |
+
import streamlit as st
|
119 |
+
import cv2
|
120 |
+
import numpy as np
|
121 |
+
from PIL import Image
|
122 |
+
|
123 |
+
# Title with emojis
|
124 |
+
st.title("πΌοΈ Image Processing\n(π Comparison View)")
|
125 |
+
|
126 |
+
# Sidebar for image upload and operation selection with emojis
|
127 |
+
with st.sidebar:
|
128 |
+
st.write("π€ Upload & Select")
|
129 |
+
uploaded_file = st.file_uploader("π Upload an Image", type=["png", "jpg", "jpeg"])
|
130 |
+
option = st.selectbox("π οΈ Choose a comparison:", [
|
131 |
+
"π« None", "π² Convert to Grayscale", "π Rotate Image", "π«οΈ Blur Image",
|
132 |
+
"π Convert to Color Space", "βοΈ Edge Detection"
|
133 |
+
])
|
134 |
+
|
135 |
+
# Function to process the image
|
136 |
+
def process_image(image, operation, value=None, extra_value=None):
|
137 |
+
if operation == "π« None":
|
138 |
+
return image
|
139 |
+
elif operation == "π² Convert to Grayscale":
|
140 |
+
return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
141 |
+
elif operation == "π Rotate Image":
|
142 |
+
if value is not None:
|
143 |
+
(h, w) = image.shape[:2]
|
144 |
+
center = (w // 2, h // 2)
|
145 |
+
matrix = cv2.getRotationMatrix2D(center, value, 1.0)
|
146 |
+
return cv2.warpAffine(image, matrix, (w, h))
|
147 |
+
elif operation == "π«οΈ Blur Image":
|
148 |
+
if value is not None:
|
149 |
+
kernel_size = (value * 2 + 1, value * 2 + 1) # Ensure odd kernel size
|
150 |
+
return cv2.GaussianBlur(image, kernel_size, 0)
|
151 |
+
elif operation == "π Convert to Color Space":
|
152 |
+
if value == "RGB":
|
153 |
+
return image # Already in RGB
|
154 |
+
elif value == "BGR2RGB":
|
155 |
+
return cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
156 |
+
elif value == "Grayscale":
|
157 |
+
return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
158 |
+
elif operation == "βοΈ Edge Detection":
|
159 |
+
if value is not None and extra_value is not None:
|
160 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
161 |
+
return cv2.Canny(gray_image, value, extra_value)
|
162 |
+
return image
|
163 |
+
|
164 |
+
if uploaded_file is not None:
|
165 |
+
# Read image
|
166 |
+
image = Image.open(uploaded_file)
|
167 |
+
img_array = np.array(image)
|
168 |
+
|
169 |
+
processed_img = img_array.copy()
|
170 |
+
|
171 |
+
# Operation-specific controls in the main area with emojis
|
172 |
+
if option == "π Rotate Image":
|
173 |
+
angle = st.slider("βͺοΈ Select Rotation Angle", -180, 180, 0)
|
174 |
+
processed_img = process_image(img_array, option, angle)
|
175 |
+
elif option == "π«οΈ Blur Image":
|
176 |
+
blur_level = st.slider("π¨ Select Blur Level", 1, 20, 5)
|
177 |
+
processed_img = process_image(img_array, option, blur_level)
|
178 |
+
elif option == "π Convert to Color Space":
|
179 |
+
color_space = st.selectbox("π¨ Choose a color space:", ["RGB", "BGR2RGB", "Grayscale"])
|
180 |
+
processed_img = process_image(img_array, option, color_space)
|
181 |
+
elif option == "βοΈ Edge Detection":
|
182 |
+
low_threshold = st.slider("π½ Lower Threshold", 0, 255, 50)
|
183 |
+
high_threshold = st.slider("πΌ Upper Threshold", 0, 255, 150)
|
184 |
+
processed_img = process_image(img_array, option, low_threshold, high_threshold)
|
185 |
+
else:
|
186 |
+
processed_img = process_image(img_array, option)
|
187 |
+
|
188 |
+
# Display images side by side with emojis in captions
|
189 |
+
col1, col2 = st.columns(2)
|
190 |
+
with col1:
|
191 |
+
st.image(image, caption="π Original Image", use_container_width=True)
|
192 |
+
|
193 |
+
with col2:
|
194 |
+
# Convert processed image to appropriate format for display
|
195 |
+
if len(processed_img.shape) == 2: # Grayscale image
|
196 |
+
processed_img_rgb = cv2.cvtColor(processed_img, cv2.COLOR_GRAY2RGB)
|
197 |
+
else:
|
198 |
+
processed_img_rgb = processed_img # Already in RGB
|
199 |
+
|
200 |
+
# Dynamic caption with emojis based on operation
|
201 |
+
caption = "π« No Processing Applied" # Default caption
|
202 |
+
if option == "π« None":
|
203 |
+
caption = "π« No Processing Applied"
|
204 |
+
elif option == "π² Convert to Grayscale":
|
205 |
+
caption = "π² Grayscale Image"
|
206 |
+
elif option == "π Rotate Image":
|
207 |
+
caption = f"π Rotated by {angle}Β°"
|
208 |
+
elif option == "π«οΈ Blur Image":
|
209 |
+
caption = f"π«οΈ Blurred (Level {blur_level})"
|
210 |
+
elif option == "π Convert to Color Space":
|
211 |
+
caption = f"π {color_space} Image"
|
212 |
+
elif option == "βοΈ Edge Detection":
|
213 |
+
caption = "βοΈ Edge Detection (Canny)"
|
214 |
+
|
215 |
+
st.image(processed_img_rgb, caption=caption, use_container_width=True)
|
216 |
+
|
217 |
+
# Download button with emoji
|
218 |
+
if len(processed_img.shape) == 2:
|
219 |
+
processed_img_download = cv2.cvtColor(processed_img, cv2.COLOR_GRAY2BGR)
|
220 |
+
else:
|
221 |
+
processed_img_download = cv2.cvtColor(processed_img, cv2.COLOR_RGB2BGR)
|
222 |
+
|
223 |
+
is_success, buffer = cv2.imencode(".png", processed_img_download)
|
224 |
+
if is_success:
|
225 |
+
st.download_button(
|
226 |
+
label="πΎ Download Processed Image",
|
227 |
+
data=buffer.tobytes(),
|
228 |
+
file_name="processed_image.png",
|
229 |
+
mime="image/png"
|
230 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
opencv-python
|
3 |
+
numpy
|
4 |
+
pillow
|