-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
112 lines (93 loc) · 3.19 KB
/
app.py
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
#image_processing_application
import streamlit as st
import cv2
import numpy as np
from PIL import Image
# Set the page layout
st.set_page_config(
page_title="Image Processing App",
page_icon=":camera:",
layout="centered",
initial_sidebar_state="expanded",
)
# Style the app
st.markdown(
"""
<style>
.main {
background-color: #000000;
color: white;
}
h1, h2, h3, h4 {
color: #1E3A8A;
}
.css-10trblm {
color: white;
}
</style>
""",
unsafe_allow_html=True,
)
# Helper functions
def apply_blur(image, ksize=(5, 5)):
return cv2.GaussianBlur(np.array(image), ksize, 0)
def apply_sharpen(image):
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
return cv2.filter2D(np.array(image), -1, kernel)
def apply_edge_detection(image):
return cv2.Canny(np.array(image), 100, 200)
def apply_thresholding(image, threshold=127):
gray_image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
_, binary_image = cv2.threshold(gray_image, threshold, 255, cv2.THRESH_BINARY)
return binary_image
def adjust_brightness(image, value=30):
hsv = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
v = cv2.add(v, value)
v[v > 255] = 255
v[v < 0] = 0
final_hsv = cv2.merge((h, s, v))
return cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
def adjust_contrast(image, alpha=1.5):
return cv2.convertScaleAbs(np.array(image), alpha=alpha, beta=0)
def adjust_saturation(image, value=30):
hsv = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
s = cv2.add(s, value)
s[s > 255] = 255
s[s < 0] = 0
final_hsv = cv2.merge((h, s, v))
return cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
# App title
st.title("Image Processing App")
# Upload image
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
if uploaded_image is not None:
image = Image.open(uploaded_image)
st.image(image, caption='Uploaded Image', use_column_width=True)
# Choose processing option
option = st.selectbox(
"Choose an Image Processing Technique",
["None", "Blur", "Sharpen", "Edge Detection", "Thresholding", "Brightness", "Contrast", "Saturation"]
)
processed_image = None
if option == "Blur":
processed_image = apply_blur(image)
elif option == "Sharpen":
processed_image = apply_sharpen(image)
elif option == "Edge Detection":
processed_image = apply_edge_detection(image)
elif option == "Thresholding":
threshold = st.slider("Threshold", 0, 255, 127)
processed_image = apply_thresholding(image, threshold)
elif option == "Brightness":
value = st.slider("Brightness", -100, 100, 30)
processed_image = adjust_brightness(image, value)
elif option == "Contrast":
alpha = st.slider("Contrast", 0.0, 3.0, 1.5)
processed_image = adjust_contrast(image, alpha)
elif option == "Saturation":
value = st.slider("Saturation", -100, 100, 30)
processed_image = adjust_saturation(image, value)
if processed_image is not None:
st.image(processed_image, caption='Processed Image', use_column_width=True)