engrharis commited on
Commit
bc9ddec
·
verified ·
1 Parent(s): 59b6bea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -50
app.py CHANGED
@@ -1,62 +1,80 @@
1
  import streamlit as st
2
- from PIL import Image, ImageEnhance, ImageFilter
3
  import numpy as np
 
4
  from io import BytesIO
5
 
6
- # Function to apply the filter
7
  def apply_filter(img):
8
- # Convert image to editable format
9
- img = img.convert("RGB")
10
-
11
- # Apply exposure
12
- exposure = 1 + (-0.31) # Adjusted for Brightness
13
- enhancer = ImageEnhance.Brightness(img)
14
- img = enhancer.enhance(exposure)
15
-
16
- # Apply contrast
17
- contrast = 1 + (-18 / 100) # Adjusted for Contrast
18
- enhancer = ImageEnhance.Contrast(img)
19
- img = enhancer.enhance(contrast)
20
-
21
- # Highlights and shadows approximation
22
- img_array = np.array(img, dtype=np.float32) / 255.0
23
- shadows = 1.5 # Adjusted for Shadows (+50)
24
- highlights = 0.0 # Adjusted for Highlights (-100)
25
- img_array = np.clip(img_array * shadows + highlights, 0, 1)
26
- img = Image.fromarray((img_array * 255).astype(np.uint8))
27
-
28
- # Whites and blacks approximation
29
- whites = 1.12 # Adjusted for Whites (+12)
30
- blacks = 0.80 # Adjusted for Blacks (-20)
31
- img_array = np.clip(img_array * whites * blacks, 0, 1)
32
- img = Image.fromarray((img_array * 255).astype(np.uint8))
33
-
34
- # Vibrance and saturation
35
- img_hsv = img.convert("HSV")
36
- h, s, v = img_hsv.split()
37
- vibrance = 1 + 70 / 100
38
- s = s.point(lambda i: min(255, max(0, int(i * vibrance))))
39
- saturation = 1 + 20 / 100
40
- s = s.point(lambda i: min(255, max(0, int(i * saturation))))
41
- img = Image.merge("HSV", (h, s, v)).convert("RGB")
42
-
43
- # Texture approximation
44
- texture = 1 + 4 / 100
45
- enhancer = ImageEnhance.Sharpness(img)
46
- img = enhancer.enhance(texture)
47
-
48
- # Sharpening using UnsharpMask
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  radius = 1.2
50
- detail = 1 + 30 / 100
51
- img = img.filter(
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
- st.image(filtered_img, use_column_width=True)
 
 
 
76
 
77
  # Provide download button
78
  buf = BytesIO()
79
- filtered_img.save(buf, format="JPEG")
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",