Commit
Β·
d6e2fe2
0
Parent(s):
deploy
Browse files- README.md +10 -0
- app.py +119 -0
- requirements.txt +4 -0
README.md
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Image Processing Streamlit
|
3 |
+
emoji: π
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: blue
|
6 |
+
sdk: streamlit
|
7 |
+
sdk_version: 1.42.2
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
---
|
app.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Function to process the image
|
7 |
+
def process_image(image, operation, value=None):
|
8 |
+
if operation == "πΌ Original":
|
9 |
+
return image
|
10 |
+
elif operation == "β« Convert to Grayscale":
|
11 |
+
return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
12 |
+
elif operation == "π Convert to Color":
|
13 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
14 |
+
elif operation == "π Rotate":
|
15 |
+
if value is not None:
|
16 |
+
return rotate_image_without_cropping(image, value)
|
17 |
+
elif operation == "π Blur":
|
18 |
+
if value is not None:
|
19 |
+
kernel_size = (value, value)
|
20 |
+
return cv2.GaussianBlur(image, kernel_size, 0)
|
21 |
+
elif operation == "β Edge Detection":
|
22 |
+
return cv2.Canny(image, 100, 200)
|
23 |
+
elif operation == "β Adjust Brightness":
|
24 |
+
if value is not None:
|
25 |
+
hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
|
26 |
+
h, s, v = cv2.split(hsv)
|
27 |
+
v = np.clip(v.astype(np.int16) + value, 0, 255).astype(np.uint8)
|
28 |
+
final_hsv = cv2.merge((h, s, v))
|
29 |
+
return cv2.cvtColor(final_hsv, cv2.COLOR_HSV2RGB)
|
30 |
+
return image
|
31 |
+
|
32 |
+
# Fix rotation issue (avoids cropping & mirroring)
|
33 |
+
def rotate_image_without_cropping(image, angle):
|
34 |
+
(h, w) = image.shape[:2]
|
35 |
+
center = (w // 2, h // 2)
|
36 |
+
|
37 |
+
# Compute the rotation matrix
|
38 |
+
M = cv2.getRotationMatrix2D(center, angle, 1.0)
|
39 |
+
|
40 |
+
# Compute the new bounding box size
|
41 |
+
cos = np.abs(M[0, 0])
|
42 |
+
sin = np.abs(M[0, 1])
|
43 |
+
new_w = int((h * sin) + (w * cos))
|
44 |
+
new_h = int((h * cos) + (w * sin))
|
45 |
+
|
46 |
+
# Adjust transformation matrix to fit in new bounds
|
47 |
+
M[0, 2] += (new_w / 2) - center[0]
|
48 |
+
M[1, 2] += (new_h / 2) - center[1]
|
49 |
+
|
50 |
+
# Apply rotation with expanded borders
|
51 |
+
rotated = cv2.warpAffine(image, M, (new_w, new_h), borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0))
|
52 |
+
|
53 |
+
return rotated
|
54 |
+
|
55 |
+
# Streamlit app
|
56 |
+
def main():
|
57 |
+
st.title(" Image Processing App ")
|
58 |
+
st.write("π Upload an image and apply various transformations below! β¨")
|
59 |
+
|
60 |
+
uploaded_file = st.file_uploader("π€ Choose an image...", type=["jpg", "jpeg", "png"])
|
61 |
+
|
62 |
+
if uploaded_file is not None:
|
63 |
+
# Read image
|
64 |
+
image = Image.open(uploaded_file)
|
65 |
+
img_array = np.array(image)
|
66 |
+
img_bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
67 |
+
|
68 |
+
col1, col2 = st.columns(2)
|
69 |
+
|
70 |
+
with col1:
|
71 |
+
st.subheader("π Original Image")
|
72 |
+
st.image(image, use_container_width=True)
|
73 |
+
|
74 |
+
with col2:
|
75 |
+
st.subheader("π Processed Image")
|
76 |
+
|
77 |
+
operation = st.selectbox(
|
78 |
+
"π Select an Operation",
|
79 |
+
["πΌ Original", "β« Convert to Grayscale", "π Convert to Color",
|
80 |
+
"π Rotate", "π Blur", "β Edge Detection", "β Adjust Brightness"]
|
81 |
+
)
|
82 |
+
|
83 |
+
processed_img = img_bgr.copy()
|
84 |
+
|
85 |
+
if operation == "π Rotate":
|
86 |
+
angle = st.slider("β© Rotation Angle", -180, 180, 0)
|
87 |
+
processed_img = process_image(img_bgr, operation, angle)
|
88 |
+
elif operation == "β Adjust Brightness":
|
89 |
+
brightness = st.slider("π Brightness Level", -100, 100, 0)
|
90 |
+
processed_img = process_image(img_bgr, operation, brightness)
|
91 |
+
elif operation == "π Blur":
|
92 |
+
blur_level = st.slider("π Blur Intensity", 1, 25, 5, step=2) # Ensures odd kernel size
|
93 |
+
processed_img = process_image(img_bgr, operation, blur_level)
|
94 |
+
else:
|
95 |
+
processed_img = process_image(img_bgr, operation)
|
96 |
+
|
97 |
+
# Convert back to RGB
|
98 |
+
if len(processed_img.shape) == 2:
|
99 |
+
processed_img_rgb = cv2.cvtColor(processed_img, cv2.COLOR_GRAY2RGB)
|
100 |
+
else:
|
101 |
+
processed_img_rgb = cv2.cvtColor(processed_img, cv2.COLOR_BGR2RGB)
|
102 |
+
|
103 |
+
st.image(processed_img_rgb, use_container_width=True)
|
104 |
+
|
105 |
+
# Ensure grayscale images are properly handled before download
|
106 |
+
if len(processed_img.shape) == 2:
|
107 |
+
processed_img = cv2.cvtColor(processed_img, cv2.COLOR_GRAY2BGR)
|
108 |
+
|
109 |
+
is_success, buffer = cv2.imencode(".png", processed_img)
|
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 |
+
if __name__ == "__main__":
|
119 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit>=1.0.0
|
2 |
+
opencv-python>=4.5.0
|
3 |
+
numpy>=1.19.0
|
4 |
+
Pillow>=8.0.0
|