Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,80 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
import numpy as np
|
|
|
4 |
from io import BytesIO
|
5 |
|
6 |
-
# Function to apply
|
7 |
def apply_filter(img):
|
8 |
-
# Convert
|
9 |
-
img =
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
img =
|
15 |
-
|
16 |
-
#
|
17 |
-
contrast =
|
18 |
-
|
19 |
-
img =
|
20 |
-
|
21 |
-
# Highlights and
|
22 |
-
|
23 |
-
shadows =
|
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 |
radius = 1.2
|
50 |
-
|
51 |
-
img = img.
|
52 |
-
ImageFilter.UnsharpMask(radius=radius, percent=int(detail * 100), threshold=3)
|
53 |
-
)
|
54 |
|
55 |
return img
|
56 |
|
57 |
# Streamlit app
|
58 |
st.title("Classic Photo Editor")
|
59 |
-
st.write("Upload an image to apply the filter.")
|
60 |
|
61 |
# Upload image
|
62 |
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
|
@@ -72,11 +90,14 @@ if uploaded_file:
|
|
72 |
# Apply the filter
|
73 |
st.subheader("Filtered Image")
|
74 |
filtered_img = apply_filter(img)
|
75 |
-
|
|
|
|
|
|
|
76 |
|
77 |
# Provide download button
|
78 |
buf = BytesIO()
|
79 |
-
|
80 |
byte_im = buf.getvalue()
|
81 |
st.download_button(
|
82 |
label="Download Filtered Image",
|
|
|
1 |
import streamlit as st
|
2 |
+
import cv2
|
3 |
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
from io import BytesIO
|
6 |
|
7 |
+
# Function to apply Lightroom-like filters
|
8 |
def apply_filter(img):
|
9 |
+
# Convert PIL Image to OpenCV format
|
10 |
+
img = np.array(img)
|
11 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
12 |
+
|
13 |
+
# Exposure: Adjust brightness
|
14 |
+
exposure = -0.31 # Negative for darker, positive for brighter
|
15 |
+
img = cv2.convertScaleAbs(img, alpha=1.0, beta=exposure * 100)
|
16 |
+
|
17 |
+
# Contrast
|
18 |
+
contrast = -18
|
19 |
+
alpha = 1 + contrast / 100.0 # Scaling factor
|
20 |
+
img = cv2.convertScaleAbs(img, alpha=alpha, beta=0)
|
21 |
+
|
22 |
+
# Highlights and Shadows
|
23 |
+
highlights = -100
|
24 |
+
shadows = 50
|
25 |
+
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
26 |
+
v = hsv[:, :, 2]
|
27 |
+
v = np.clip(v * (1 + shadows / 100.0) + highlights, 0, 255).astype(np.uint8)
|
28 |
+
hsv[:, :, 2] = v
|
29 |
+
img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
30 |
+
|
31 |
+
# Whites and Blacks
|
32 |
+
whites = 12
|
33 |
+
blacks = -20
|
34 |
+
img = np.clip(img + whites, 0, 255).astype(np.uint8)
|
35 |
+
img = np.clip(img + blacks, 0, 255).astype(np.uint8)
|
36 |
+
|
37 |
+
# Temperature and Tint
|
38 |
+
temp = -2 # Blue (+ve) or Yellow (-ve)
|
39 |
+
tint = 2 # Green (-ve) or Magenta (+ve)
|
40 |
+
b, g, r = cv2.split(img)
|
41 |
+
b = np.clip(b + temp, 0, 255).astype(np.uint8)
|
42 |
+
r = np.clip(r + tint, 0, 255).astype(np.uint8)
|
43 |
+
img = cv2.merge((b, g, r))
|
44 |
+
|
45 |
+
# Vibrance
|
46 |
+
vibrance = 70
|
47 |
+
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
|
48 |
+
l, a, b = cv2.split(lab)
|
49 |
+
a = np.clip(a * (1 + vibrance / 100.0), 0, 255).astype(np.uint8)
|
50 |
+
b = np.clip(b * (1 + vibrance / 100.0), 0, 255).astype(np.uint8)
|
51 |
+
img = cv2.merge((l, a, b))
|
52 |
+
img = cv2.cvtColor(img, cv2.COLOR_LAB2BGR)
|
53 |
+
|
54 |
+
# Saturation
|
55 |
+
saturation = 20
|
56 |
+
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
57 |
+
s = hsv[:, :, 1]
|
58 |
+
s = np.clip(s * (1 + saturation / 100.0), 0, 255).astype(np.uint8)
|
59 |
+
hsv[:, :, 1] = s
|
60 |
+
img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
61 |
+
|
62 |
+
# Texture (Clarity approximation)
|
63 |
+
texture = 4
|
64 |
+
kernel = np.array([[-1, -1, -1], [-1, 8 + texture, -1], [-1, -1, -1]])
|
65 |
+
img = cv2.filter2D(img, -1, kernel)
|
66 |
+
|
67 |
+
# Sharpening
|
68 |
+
sharpening = 18
|
69 |
radius = 1.2
|
70 |
+
gaussian = cv2.GaussianBlur(img, (0, 0), sigmaX=radius)
|
71 |
+
img = cv2.addWeighted(img, 1 + sharpening / 100.0, gaussian, -sharpening / 100.0, 0)
|
|
|
|
|
72 |
|
73 |
return img
|
74 |
|
75 |
# Streamlit app
|
76 |
st.title("Classic Photo Editor")
|
77 |
+
st.write("Upload an image to apply the Lightroom-like filter.")
|
78 |
|
79 |
# Upload image
|
80 |
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
|
|
|
90 |
# Apply the filter
|
91 |
st.subheader("Filtered Image")
|
92 |
filtered_img = apply_filter(img)
|
93 |
+
|
94 |
+
# Convert back to PIL Image for display
|
95 |
+
filtered_pil = Image.fromarray(cv2.cvtColor(filtered_img, cv2.COLOR_BGR2RGB))
|
96 |
+
st.image(filtered_pil, use_column_width=True)
|
97 |
|
98 |
# Provide download button
|
99 |
buf = BytesIO()
|
100 |
+
filtered_pil.save(buf, format="JPEG")
|
101 |
byte_im = buf.getvalue()
|
102 |
st.download_button(
|
103 |
label="Download Filtered Image",
|