Repcak00 commited on
Commit
6fcbd64
·
verified ·
1 Parent(s): 23924ec

feat: add logging

Browse files
Files changed (1) hide show
  1. src/utils.py +160 -155
src/utils.py CHANGED
@@ -1,155 +1,160 @@
1
- from PIL import Image, ImageEnhance
2
- from rembg import remove
3
-
4
-
5
- def load_and_process_image(
6
- uploaded_file,
7
- session,
8
- transparency_mode,
9
- rgb_color,
10
- add_watermark=False,
11
- watermark_file=None,
12
- wm_opacity=0.15,
13
- wm_scale=0.33,
14
- wm_position="bottom-right",
15
- ):
16
-
17
- input_image = Image.open(uploaded_file)
18
-
19
- # Remove background
20
- output = remove(input_image, session=session)
21
- watermark_img = Image.open(watermark_file) if watermark_file else None
22
- # Handle transparency
23
- if transparency_mode == "Keep transparent":
24
- processed = output
25
- else:
26
- background = Image.new("RGB", output.size, rgb_color)
27
- background.paste(output, (0, 0), output)
28
- processed = background
29
-
30
- # Apply watermark if enabled
31
- if add_watermark and watermark_img is not None:
32
- processed = apply_watermark(
33
- processed, watermark_img, opacity=wm_opacity, scale=wm_scale, position=wm_position
34
- )
35
-
36
- return input_image, processed
37
-
38
-
39
- def apply_watermark0(base_img, watermark_img, opacity=0.15, scale=0.33, position="bottom-right"):
40
- # Ensure watermark has alpha
41
- watermark = watermark_img.convert("RGBA")
42
-
43
- # Resize watermark relative to base image width
44
- base_w, base_h = base_img.size
45
- new_w = int(base_w * scale)
46
- aspect_ratio = watermark.height / watermark.width
47
- new_h = int(new_w * aspect_ratio)
48
- watermark = watermark.resize((new_w, new_h), Image.LANCZOS)
49
-
50
- # Apply opacity
51
- alpha = watermark.split()[3]
52
- alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
53
- watermark.putalpha(alpha)
54
-
55
- # Determine position
56
- if position == "bottom-right":
57
- pos = (base_w - new_w - 10, base_h - new_h - 10)
58
- elif position == "bottom-left":
59
- pos = (10, base_h - new_h - 10)
60
- elif position == "top-right":
61
- pos = (base_w - new_w - 10, 10)
62
- else: # top-left
63
- pos = (10, 10)
64
-
65
- # Composite watermark
66
- base_img = base_img.convert("RGBA")
67
- base_img.paste(watermark, pos, watermark)
68
-
69
- return base_img
70
-
71
-
72
- def apply_watermark(
73
- base_img,
74
- watermark_img,
75
- opacity=0.15,
76
- scale=0.33,
77
- position="bottom-right",
78
- margin=10,
79
- ):
80
- """
81
- Apply watermark with pure transparency - no color changes.
82
-
83
- Args:
84
- opacity: 0.0 (fully transparent) to 1.0 (fully opaque)
85
- """
86
- base = base_img.convert("RGBA")
87
- wm = watermark_img.convert("RGBA")
88
-
89
- # Resize watermark relative to base width
90
- base_w, base_h = base.size
91
- new_w = int(base_w * scale)
92
- aspect = wm.height / wm.width
93
- new_h = int(new_w * aspect)
94
- wm = wm.resize((new_w, new_h), Image.LANCZOS)
95
-
96
- # Apply transparency to alpha channel ONLY - colors remain untouched
97
- r, g, b, a = wm.split()
98
- # Multiply alpha by opacity (keeps RGB exactly the same)
99
- a = a.point(lambda p: int(p * opacity))
100
- wm = Image.merge("RGBA", (r, g, b, a))
101
-
102
- # Position
103
- if position == "bottom-right":
104
- pos = (base_w - new_w - margin, base_h - new_h - margin)
105
- elif position == "bottom-left":
106
- pos = (margin, base_h - new_h - margin)
107
- elif position == "top-right":
108
- pos = (base_w - new_w - margin, margin)
109
- else: # top-left
110
- pos = (margin, margin)
111
-
112
- # Composite using alpha_composite for proper blending
113
- layer = Image.new("RGBA", base.size, (0, 0, 0, 0))
114
- layer.paste(wm, pos)
115
-
116
- return Image.alpha_composite(base, layer)
117
-
118
-
119
- def apply_watermark2(base_img, watermark_img, opacity=0.15, scale=0.33, position="bottom-right"):
120
- # Ensure base image is RGBA
121
- base = base_img.convert("RGBA")
122
-
123
- # Ensure watermark is RGBA
124
- wm = watermark_img.convert("RGBA")
125
-
126
- # Resize watermark relative to base width
127
- base_w, base_h = base.size
128
- new_w = int(base_w * scale)
129
- aspect = wm.height / wm.width
130
- new_h = int(new_w * aspect)
131
- wm = wm.resize((new_w, new_h), Image.LANCZOS)
132
-
133
- # Apply opacity WITHOUT altering colors
134
- # Extract alpha channel
135
- alpha = wm.split()[3]
136
- # Adjust brightness of alpha only
137
- alpha = ImageEnhance.Brightness(alpha).enhance(1 - opacity)
138
- wm.putalpha(alpha)
139
-
140
- # Determine position
141
- if position == "bottom-right":
142
- pos = (base_w - new_w - 10, base_h - new_h - 10)
143
- elif position == "bottom-left":
144
- pos = (10, base_h - new_h - 10)
145
- elif position == "top-right":
146
- pos = (base_w - new_w - 10, 10)
147
- else: # top-left
148
- pos = (10, 10)
149
-
150
- # Composite watermark onto base
151
- combined = Image.new("RGBA", base.size)
152
- combined.paste(base, (0, 0))
153
- combined.paste(wm, pos, wm)
154
-
155
- return combined.convert("RGBA")
 
 
 
 
 
 
1
+ from PIL import Image, ImageEnhance
2
+ from rembg import remove
3
+
4
+ from logger import get_logger
5
+
6
+ logger = get_logger("image_utils")
7
+
8
+
9
+ def load_and_process_image(
10
+ uploaded_file,
11
+ session,
12
+ transparency_mode,
13
+ rgb_color,
14
+ add_watermark=False,
15
+ watermark_file=None,
16
+ wm_opacity=0.15,
17
+ wm_scale=0.33,
18
+ wm_position="bottom-right",
19
+ ):
20
+ logger.info(f"Opening image: {uploaded_file.name}")
21
+
22
+ input_image = Image.open(uploaded_file)
23
+
24
+ # Remove background
25
+ output = remove(input_image, session=session)
26
+ watermark_img = Image.open(watermark_file) if watermark_file else None
27
+ # Handle transparency
28
+ if transparency_mode == "Keep transparent":
29
+ processed = output
30
+ else:
31
+ background = Image.new("RGB", output.size, rgb_color)
32
+ background.paste(output, (0, 0), output)
33
+ processed = background
34
+
35
+ # Apply watermark if enabled
36
+ if add_watermark and watermark_img is not None:
37
+ processed = apply_watermark(
38
+ processed, watermark_img, opacity=wm_opacity, scale=wm_scale, position=wm_position
39
+ )
40
+
41
+ return input_image, processed
42
+
43
+
44
+ def apply_watermark0(base_img, watermark_img, opacity=0.15, scale=0.33, position="bottom-right"):
45
+ # Ensure watermark has alpha
46
+ watermark = watermark_img.convert("RGBA")
47
+
48
+ # Resize watermark relative to base image width
49
+ base_w, base_h = base_img.size
50
+ new_w = int(base_w * scale)
51
+ aspect_ratio = watermark.height / watermark.width
52
+ new_h = int(new_w * aspect_ratio)
53
+ watermark = watermark.resize((new_w, new_h), Image.LANCZOS)
54
+
55
+ # Apply opacity
56
+ alpha = watermark.split()[3]
57
+ alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
58
+ watermark.putalpha(alpha)
59
+
60
+ # Determine position
61
+ if position == "bottom-right":
62
+ pos = (base_w - new_w - 10, base_h - new_h - 10)
63
+ elif position == "bottom-left":
64
+ pos = (10, base_h - new_h - 10)
65
+ elif position == "top-right":
66
+ pos = (base_w - new_w - 10, 10)
67
+ else: # top-left
68
+ pos = (10, 10)
69
+
70
+ # Composite watermark
71
+ base_img = base_img.convert("RGBA")
72
+ base_img.paste(watermark, pos, watermark)
73
+
74
+ return base_img
75
+
76
+
77
+ def apply_watermark(
78
+ base_img,
79
+ watermark_img,
80
+ opacity=0.15,
81
+ scale=0.33,
82
+ position="bottom-right",
83
+ margin=10,
84
+ ):
85
+ """
86
+ Apply watermark with pure transparency - no color changes.
87
+
88
+ Args:
89
+ opacity: 0.0 (fully transparent) to 1.0 (fully opaque)
90
+ """
91
+ base = base_img.convert("RGBA")
92
+ wm = watermark_img.convert("RGBA")
93
+
94
+ # Resize watermark relative to base width
95
+ base_w, base_h = base.size
96
+ new_w = int(base_w * scale)
97
+ aspect = wm.height / wm.width
98
+ new_h = int(new_w * aspect)
99
+ wm = wm.resize((new_w, new_h), Image.LANCZOS)
100
+
101
+ # Apply transparency to alpha channel ONLY - colors remain untouched
102
+ r, g, b, a = wm.split()
103
+ # Multiply alpha by opacity (keeps RGB exactly the same)
104
+ a = a.point(lambda p: int(p * opacity))
105
+ wm = Image.merge("RGBA", (r, g, b, a))
106
+
107
+ # Position
108
+ if position == "bottom-right":
109
+ pos = (base_w - new_w - margin, base_h - new_h - margin)
110
+ elif position == "bottom-left":
111
+ pos = (margin, base_h - new_h - margin)
112
+ elif position == "top-right":
113
+ pos = (base_w - new_w - margin, margin)
114
+ else: # top-left
115
+ pos = (margin, margin)
116
+
117
+ # Composite using alpha_composite for proper blending
118
+ layer = Image.new("RGBA", base.size, (0, 0, 0, 0))
119
+ layer.paste(wm, pos)
120
+
121
+ return Image.alpha_composite(base, layer)
122
+
123
+
124
+ def apply_watermark2(base_img, watermark_img, opacity=0.15, scale=0.33, position="bottom-right"):
125
+ # Ensure base image is RGBA
126
+ base = base_img.convert("RGBA")
127
+
128
+ # Ensure watermark is RGBA
129
+ wm = watermark_img.convert("RGBA")
130
+
131
+ # Resize watermark relative to base width
132
+ base_w, base_h = base.size
133
+ new_w = int(base_w * scale)
134
+ aspect = wm.height / wm.width
135
+ new_h = int(new_w * aspect)
136
+ wm = wm.resize((new_w, new_h), Image.LANCZOS)
137
+
138
+ # Apply opacity WITHOUT altering colors
139
+ # Extract alpha channel
140
+ alpha = wm.split()[3]
141
+ # Adjust brightness of alpha only
142
+ alpha = ImageEnhance.Brightness(alpha).enhance(1 - opacity)
143
+ wm.putalpha(alpha)
144
+
145
+ # Determine position
146
+ if position == "bottom-right":
147
+ pos = (base_w - new_w - 10, base_h - new_h - 10)
148
+ elif position == "bottom-left":
149
+ pos = (10, base_h - new_h - 10)
150
+ elif position == "top-right":
151
+ pos = (base_w - new_w - 10, 10)
152
+ else: # top-left
153
+ pos = (10, 10)
154
+
155
+ # Composite watermark onto base
156
+ combined = Image.new("RGBA", base.size)
157
+ combined.paste(base, (0, 0))
158
+ combined.paste(wm, pos, wm)
159
+
160
+ return combined.convert("RGBA")