Kabila22 commited on
Commit
cf65d69
Β·
1 Parent(s): 341a2a9

Add file.txt

Browse files
Files changed (2) hide show
  1. app.py +230 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import streamlit as st
2
+ # import cv2
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