Spaces:
Running
Running
Commit
·
76524db
1
Parent(s):
be24af1
Add color and saturation filters to filters.py
Browse files- Introduced warm and cool color effect filters
- Added saturation adjustment filter
- Implemented intensity/factor controls for each filter
- Used OpenCV for color channel manipulation
- Registered new filters with default and range parameters
- filters.py +102 -0
filters.py
CHANGED
@@ -143,3 +143,105 @@ def sketch_effect(image):
|
|
143 |
sketch = cv2.divide(gray, 255 - blurred, scale=256)
|
144 |
|
145 |
return sketch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
sketch = cv2.divide(gray, 255 - blurred, scale=256)
|
144 |
|
145 |
return sketch
|
146 |
+
|
147 |
+
|
148 |
+
@registry.register("Warm", defaults={
|
149 |
+
"intensity": 30,
|
150 |
+
}, min_vals={
|
151 |
+
"intensity": 0,
|
152 |
+
}, max_vals={
|
153 |
+
"intensity": 100,
|
154 |
+
}, step_vals={
|
155 |
+
"intensity": 1,
|
156 |
+
})
|
157 |
+
def warm_filter(image, intensity: int = 30):
|
158 |
+
"""
|
159 |
+
## Adds a warm color effect to the image.
|
160 |
+
|
161 |
+
**Args:**
|
162 |
+
* `image` (numpy.ndarray): Input image (BGR)
|
163 |
+
* `intensity` (int): Intensity of the warm effect (0-100)
|
164 |
+
|
165 |
+
**Returns:**
|
166 |
+
* `numpy.ndarray`: Image with warm color effect
|
167 |
+
"""
|
168 |
+
# Convert intensity to actual adjustment values
|
169 |
+
intensity_scale = intensity / 100.0
|
170 |
+
|
171 |
+
# Split the image into BGR channels
|
172 |
+
b, g, r = cv2.split(image.astype(np.float32))
|
173 |
+
|
174 |
+
# Increase red, slightly increase green, decrease blue
|
175 |
+
r = np.clip(r * (1 + 0.5 * intensity_scale), 0, 255)
|
176 |
+
g = np.clip(g * (1 + 0.1 * intensity_scale), 0, 255)
|
177 |
+
b = np.clip(b * (1 - 0.1 * intensity_scale), 0, 255)
|
178 |
+
|
179 |
+
return cv2.merge([b, g, r]).astype(np.uint8)
|
180 |
+
|
181 |
+
|
182 |
+
@registry.register("Cool", defaults={
|
183 |
+
"intensity": 30,
|
184 |
+
}, min_vals={
|
185 |
+
"intensity": 0,
|
186 |
+
}, max_vals={
|
187 |
+
"intensity": 100,
|
188 |
+
}, step_vals={
|
189 |
+
"intensity": 1,
|
190 |
+
})
|
191 |
+
def cool_filter(image, intensity: int = 30):
|
192 |
+
"""
|
193 |
+
## Adds a cool color effect to the image.
|
194 |
+
|
195 |
+
**Args:**
|
196 |
+
* `image` (numpy.ndarray): Input image (BGR)
|
197 |
+
* `intensity` (int): Intensity of the cool effect (0-100)
|
198 |
+
|
199 |
+
**Returns:**
|
200 |
+
* `numpy.ndarray`: Image with cool color effect
|
201 |
+
"""
|
202 |
+
# Convert intensity to actual adjustment values
|
203 |
+
intensity_scale = intensity / 100.0
|
204 |
+
|
205 |
+
# Split the image into BGR channels
|
206 |
+
b, g, r = cv2.split(image.astype(np.float32))
|
207 |
+
|
208 |
+
# Increase blue, slightly increase green, decrease red
|
209 |
+
b = np.clip(b * (1 + 0.5 * intensity_scale), 0, 255)
|
210 |
+
g = np.clip(g * (1 + 0.1 * intensity_scale), 0, 255)
|
211 |
+
r = np.clip(r * (1 - 0.1 * intensity_scale), 0, 255)
|
212 |
+
|
213 |
+
return cv2.merge([b, g, r]).astype(np.uint8)
|
214 |
+
|
215 |
+
|
216 |
+
@registry.register("Saturation", defaults={
|
217 |
+
"factor": 50,
|
218 |
+
}, min_vals={
|
219 |
+
"factor": 0,
|
220 |
+
}, max_vals={
|
221 |
+
"factor": 100,
|
222 |
+
}, step_vals={
|
223 |
+
"factor": 1,
|
224 |
+
})
|
225 |
+
def adjust_saturation(image, factor: int = 50):
|
226 |
+
"""
|
227 |
+
## Adjusts the saturation of the image.
|
228 |
+
|
229 |
+
**Args:**
|
230 |
+
* `image` (numpy.ndarray): Input image (BGR)
|
231 |
+
* `factor` (int): Saturation factor (0-100, 50 is normal)
|
232 |
+
|
233 |
+
**Returns:**
|
234 |
+
* `numpy.ndarray`: Image with adjusted saturation
|
235 |
+
"""
|
236 |
+
# Convert factor to multiplication value (0.0 to 2.0)
|
237 |
+
factor = (factor / 50.0)
|
238 |
+
|
239 |
+
# Convert to HSV
|
240 |
+
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV).astype(np.float32)
|
241 |
+
|
242 |
+
# Adjust saturation
|
243 |
+
hsv[:, :, 1] = np.clip(hsv[:, :, 1] * factor, 0, 255)
|
244 |
+
|
245 |
+
# Convert back to BGR
|
246 |
+
return cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2BGR)
|
247 |
+
|