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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5900d7
8a71b8a
 
 
c5900d7
 
8a71b8a
 
 
 
 
 
 
 
 
 
c5900d7
8a71b8a
 
 
 
 
 
 
 
 
 
 
 
 
c5900d7
8a71b8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5900d7
8a71b8a
 
 
 
 
 
 
 
 
 
 
 
 
c5900d7
8a71b8a
 
 
 
 
 
 
 
 
 
 
 
 
c5900d7
8a71b8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5900d7
8a71b8a
 
 
 
 
 
 
 
 
 
 
a9a4745
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import cv2
import numpy as np
from registry import registry


@registry.register("Original")
def original(image):
    """
    ## Original Image - no filter applied.
    
    **Args:**
    * `image` (numpy.ndarray): Input image
    
    **Returns:**
    * `numpy.ndarray`: Original image
    """
    return image


@registry.register("Dot Effect", defaults={
    "dot_size": 10,
    "dot_spacing": 2,
    "invert": False,
}, min_vals={
    "dot_size": 1,
    "dot_spacing": 1,
}, max_vals={
    "dot_size": 20,
    "dot_spacing": 10,
}, step_vals={
    "dot_size": 1,
    "dot_spacing": 1,
})
def dot_effect(image, dot_size: int = 10, dot_spacing: int = 2, invert: bool = False):
    """
    ## Convert image to artistic dot pattern effect.
    
    **Args:**
    * `image` (numpy.ndarray): Input image (BGR or grayscale)
    * `dot_size` (int): Size of each dot
    * `dot_spacing` (int): Spacing between dots
    * `invert` (bool): Invert dot colors
    
    **Returns:**
    * `numpy.ndarray`: Dotted image
    """
    if len(image.shape) == 3:
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    else:
        gray = image

    gray = cv2.adaptiveThreshold(
        gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
        cv2.THRESH_BINARY, 25, 5
    )

    height, width = gray.shape
    canvas = np.zeros_like(gray) if not invert else np.full_like(gray, 255)

    y_dots = range(0, height, dot_size + dot_spacing)
    x_dots = range(0, width, dot_size + dot_spacing)

    dot_color = 255 if not invert else 0
    for y in y_dots:
        for x in x_dots:
            region = gray[y:min(y+dot_size, height), x:min(x+dot_size, width)]
            if region.size > 0:
                brightness = np.mean(region)
                relative_brightness = brightness / 255.0
                if invert:
                    relative_brightness = 1 - relative_brightness
                radius = int((dot_size/2) * relative_brightness)
                if radius > 0:
                    cv2.circle(canvas, (x + dot_size//2, y + dot_size//2),
                             radius, (dot_color), -1)

    return canvas


@registry.register("Pixelize", defaults={
    "pixel_size": 10,
}, min_vals={
    "pixel_size": 1,
}, max_vals={
    "pixel_size": 50,
}, step_vals={
    "pixel_size": 1,
})
def pixelize(image, pixel_size: int = 10):
    """
    ## Create pixelated effect (8-bit retro style).
    
    **Args:**
    * `image` (numpy.ndarray): Input image
    * `pixel_size` (int): Size of each pixel block
    
    **Returns:**
    * `numpy.ndarray`: Pixelized image
    """
    height, width = image.shape[:2]
    small_height = height // pixel_size
    small_width = width // pixel_size
    small_image = cv2.resize(image, (small_width, small_height), interpolation=cv2.INTER_LINEAR)
    pixelized_image = cv2.resize(small_image, (width, height), interpolation=cv2.INTER_NEAREST)
    return pixelized_image


@registry.register("Sketch Effect")
def sketch_effect(image):
    """
    ## Convert image to pencil sketch.
    
    **Args:**
    * `image` (numpy.ndarray): Input image
    
    **Returns:**
    * `numpy.ndarray`: Sketch effect image
    """
    if len(image.shape) == 3:
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    else:
        gray = image

    inverted_gray = cv2.bitwise_not(gray)
    blurred = cv2.GaussianBlur(inverted_gray, (21, 21), 0)
    sketch = cv2.divide(gray, 255 - blurred, scale=256)
    return sketch


@registry.register("Warm", defaults={
    "intensity": 30,
}, min_vals={
    "intensity": 0,
}, max_vals={
    "intensity": 100,
}, step_vals={
    "intensity": 1,
})
def warm_filter(image, intensity: int = 30):
    """
    ## Add warm tones to image (sunset, autumn style).
    
    **Args:**
    * `image` (numpy.ndarray): Input image (BGR)
    * `intensity` (int): Effect intensity (0-100)
    
    **Returns:**
    * `numpy.ndarray`: Warm-toned image
    """
    intensity_scale = intensity / 100.0
    b, g, r = cv2.split(image.astype(np.float32))
    r = np.clip(r * (1 + 0.5 * intensity_scale), 0, 255)
    g = np.clip(g * (1 + 0.1 * intensity_scale), 0, 255)
    b = np.clip(b * (1 - 0.1 * intensity_scale), 0, 255)
    return cv2.merge([b, g, r]).astype(np.uint8)


@registry.register("Cool", defaults={
    "intensity": 30,
}, min_vals={
    "intensity": 0,
}, max_vals={
    "intensity": 100,
}, step_vals={
    "intensity": 1,
})
def cool_filter(image, intensity: int = 30):
    """
    ## Add cool tones to image (ice, ocean style).
    
    **Args:**
    * `image` (numpy.ndarray): Input image (BGR)
    * `intensity` (int): Effect intensity (0-100)
    
    **Returns:**
    * `numpy.ndarray`: Cool-toned image
    """
    intensity_scale = intensity / 100.0
    b, g, r = cv2.split(image.astype(np.float32))
    b = np.clip(b * (1 + 0.5 * intensity_scale), 0, 255)
    g = np.clip(g * (1 + 0.1 * intensity_scale), 0, 255)
    r = np.clip(r * (1 - 0.1 * intensity_scale), 0, 255)
    return cv2.merge([b, g, r]).astype(np.uint8)


@registry.register("Saturation", defaults={
    "factor": 50,
}, min_vals={
    "factor": 0,
}, max_vals={
    "factor": 100,
}, step_vals={
    "factor": 1,
})
def adjust_saturation(image, factor: int = 50):
    """
    ## Adjust color saturation of image.
    
    **Args:**
    * `image` (numpy.ndarray): Input image (BGR)
    * `factor` (int): Saturation factor (0-100, 50 is normal)
    
    **Returns:**
    * `numpy.ndarray`: Saturation-adjusted image
    """
    factor = (factor / 50.0)
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV).astype(np.float32)
    hsv[:, :, 1] = np.clip(hsv[:, :, 1] * factor, 0, 255)
    return cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2BGR)


@registry.register("Vintage", defaults={
    "intensity": 50,
}, min_vals={
    "intensity": 0,
}, max_vals={
    "intensity": 100,
}, step_vals={
    "intensity": 1,
})
def vintage_filter(image, intensity: int = 50):
    """
    ## Create vintage/retro photo effect (70s style).
    
    **Args:**
    * `image` (numpy.ndarray): Input image (BGR)
    * `intensity` (int): Vintage effect intensity (0-100)
    
    **Returns:**
    * `numpy.ndarray`: Vintage-styled image
    """
    intensity_scale = intensity / 100.0
    b, g, r = cv2.split(image.astype(np.float32))
    r = np.clip(r * (1 + 0.3 * intensity_scale), 0, 255)
    g = np.clip(g * (1 - 0.1 * intensity_scale), 0, 255)
    b = np.clip(b * (1 - 0.2 * intensity_scale), 0, 255)
    result = cv2.merge([b, g, r]).astype(np.uint8)
    if intensity > 0:
        blur_amount = int(3 * intensity_scale) * 2 + 1
        result = cv2.GaussianBlur(result, (blur_amount, blur_amount), 0)
    return result


@registry.register("Vignette", defaults={
    "intensity": 50,
}, min_vals={
    "intensity": 0,
}, max_vals={
    "intensity": 100,
}, step_vals={
    "intensity": 1,
})
def vignette_effect(image, intensity: int = 50):
    """
    ## Add darkening effect to image corners (vignette).
    
    **Args:**
    * `image` (numpy.ndarray): Input image (BGR)
    * `intensity` (int): Vignette intensity (0-100)
    
    **Returns:**
    * `numpy.ndarray`: Vignetted image
    """
    height, width = image.shape[:2]
    X_resultant = np.abs(np.linspace(-1, 1, width)[None, :])
    Y_resultant = np.abs(np.linspace(-1, 1, height)[:, None])
    mask = np.sqrt(X_resultant**2 + Y_resultant**2)
    mask = 1 - np.clip(mask, 0, 1)
    mask = (mask - mask.min()) / (mask.max() - mask.min())
    mask = mask ** (1 + intensity/50)
    mask = mask[:, :, None]
    result = image.astype(np.float32) * mask
    return np.clip(result, 0, 255).astype(np.uint8)


@registry.register("HDR Effect", defaults={
    "strength": 50,
}, min_vals={
    "strength": 0,
}, max_vals={
    "strength": 100,
}, step_vals={
    "strength": 1,
})
def hdr_effect(image, strength: int = 50):
    """
    ## Enhance image details with HDR effect.
    
    **Args:**
    * `image` (numpy.ndarray): Input image (BGR)
    * `strength` (int): HDR strength (0-100)
    
    **Returns:**
    * `numpy.ndarray`: HDR-enhanced image
    """
    strength_scale = strength / 100.0
    lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB).astype(np.float32)
    l, a, b = cv2.split(lab)
    clahe = cv2.createCLAHE(clipLimit=3.0 * strength_scale, tileGridSize=(8, 8))
    l = clahe.apply(l.astype(np.uint8)).astype(np.float32)
    if strength > 0:
        blur = cv2.GaussianBlur(l, (0, 0), 3)
        detail = cv2.addWeighted(l, 1 + strength_scale, blur, -strength_scale, 0)
        l = cv2.addWeighted(l, 1 - strength_scale/2, detail, strength_scale/2, 0)
    enhanced_lab = cv2.merge([l, a, b])
    result = cv2.cvtColor(enhanced_lab.astype(np.uint8), cv2.COLOR_LAB2BGR)
    return result


@registry.register("Gaussian Blur", defaults={
    "kernel_size": 5,
}, min_vals={
    "kernel_size": 1,
}, max_vals={
    "kernel_size": 31,
}, step_vals={
    "kernel_size": 2,
})
def gaussian_blur(image, kernel_size: int = 5):
    """
    ## Blur image with Gaussian filter.
    
    **Args:**
    * `image` (numpy.ndarray): Input image
    * `kernel_size` (int): Kernel size (must be odd number)
    
    **Returns:**
    * `numpy.ndarray`: Blurred image
    """
    if kernel_size % 2 == 0:
        kernel_size += 1
    return cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)


@registry.register("Sharpen", defaults={
    "amount": 50,
}, min_vals={
    "amount": 0,
}, max_vals={
    "amount": 100,
}, step_vals={
    "amount": 1,
})
def sharpen(image, amount: int = 50):
    """
    ## Sharpen image details.
    
    **Args:**
    * `image` (numpy.ndarray): Input image
    * `amount` (int): Sharpening intensity (0-100)
    
    **Returns:**
    * `numpy.ndarray`: Sharpened image
    """
    amount = amount / 100.0
    kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
    sharpened = cv2.filter2D(image, -1, kernel)
    return cv2.addWeighted(image, 1 - amount, sharpened, amount, 0)


@registry.register("Emboss", defaults={
    "strength": 50,
    "direction": 0,
}, min_vals={
    "strength": 0,
    "direction": 0,
}, max_vals={
    "strength": 100,
    "direction": 7,
}, step_vals={
    "strength": 1,
    "direction": 1,
})
def emboss(image, strength: int = 50, direction: int = 0):
    """
    ## Create 3D embossed effect.
    
    **Args:**
    * `image` (numpy.ndarray): Input image
    * `strength` (int): Emboss strength (0-100)
    * `direction` (int): Light direction (0-7)
    
    **Returns:**
    * `numpy.ndarray`: Embossed image
    """
    strength = strength / 100.0 * 2.0
    kernels = [
        np.array([[-1, -1, 0], [-1, 1, 1], [0, 1, 1]]),
        np.array([[-1, 0, 1], [-1, 1, 1], [-1, 0, 1]]),
        np.array([[0, 1, 1], [-1, 1, 1], [-1, -1, 0]]),
        np.array([[1, 1, 1], [0, 1, 0], [-1, -1, -1]]),
        np.array([[1, 1, 0], [1, 1, -1], [0, -1, -1]]),
        np.array([[1, 0, -1], [1, 1, -1], [1, 0, -1]]),
        np.array([[0, -1, -1], [1, 1, -1], [1, 1, 0]]),
        np.array([[-1, -1, -1], [0, 1, 0], [1, 1, 1]])
    ]
    kernel = kernels[direction % 8]
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    embossed = cv2.filter2D(gray, -1, kernel * strength)
    embossed = cv2.normalize(embossed, None, 0, 255, cv2.NORM_MINMAX)
    return cv2.cvtColor(embossed.astype(np.uint8), cv2.COLOR_GRAY2BGR)


@registry.register("Oil Painting", defaults={
    "size": 5,
    "dynRatio": 1,
}, min_vals={
    "size": 1,
    "dynRatio": 1,
}, max_vals={
    "size": 15,
    "dynRatio": 7,
}, step_vals={
    "size": 2,
    "dynRatio": 1,
})
def oil_painting(image, size: int = 5, dynRatio: int = 1):
    """
    ## Create oil painting effect.
    
    **Args:**
    * `image` (numpy.ndarray): Input image
    * `size` (int): Processing area size
    * `dynRatio` (int): Dynamic ratio affecting color intensity
    
    **Returns:**
    * `numpy.ndarray`: Oil painting styled image
    """
    return cv2.xphoto.oilPainting(image, size, dynRatio)


@registry.register("Black and White")
def black_and_white(image):
    """
    ## Convert to classic black and white.
    
    **Args:**
    * `image` (numpy.ndarray): Input image
    
    **Returns:**
    * `numpy.ndarray`: Grayscale image
    """
    return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


@registry.register("Sepia")
def sepia(image):
    """
    ## Create sepia tone classic brown effect.
    
    **Args:**
    * `image` (numpy.ndarray): Input image
    
    **Returns:**
    * `numpy.ndarray`: Sepia-toned image
    """
    rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    sepia_matrix = np.array([
        [0.393, 0.769, 0.189],
        [0.349, 0.686, 0.168],
        [0.272, 0.534, 0.131]
    ])
    sepia_image = np.dot(rgb, sepia_matrix.T)
    sepia_image = np.clip(sepia_image, 0, 255)
    return cv2.cvtColor(sepia_image.astype(np.uint8), cv2.COLOR_RGB2BGR)


@registry.register("Negative")
def negative(image):
    """
    ## Invert colors for negative film effect.
    
    **Args:**
    * `image` (numpy.ndarray): Input image
    
    **Returns:**
    * `numpy.ndarray`: Negative image
    """
    return cv2.bitwise_not(image)


@registry.register("Watercolor")
def watercolor(image):
    """
    ## Create watercolor painting effect.
    
    **Args:**
    * `image` (numpy.ndarray): Input image
    
    **Returns:**
    * `numpy.ndarray`: Watercolor styled image
    """
    return cv2.xphoto.oilPainting(image, 7, 1)


@registry.register("Posterization")
def posterize(image):
    """
    ## Reduce colors for artistic poster effect.
    
    **Args:**
    * `image` (numpy.ndarray): Input image
    
    **Returns:**
    * `numpy.ndarray`: Posterized image
    """
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    hsv[:, :, 1] = cv2.equalizeHist(hsv[:, :, 1])
    hsv[:, :, 2] = cv2.equalizeHist(hsv[:, :, 2])
    return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)


@registry.register("Cross Process")
def cross_process(image):
    """
    ## Film cross-processing effect.
    
    **Args:**
    * `image` (numpy.ndarray): Input image
    
    **Returns:**
    * `numpy.ndarray`: Cross-processed image
    """
    b, g, r = cv2.split(image.astype(np.float32))
    b = np.clip(b * 1.2, 0, 255)
    g = np.clip(g * 0.8, 0, 255)
    r = np.clip(r * 1.4, 0, 255)
    return cv2.merge([b, g, r]).astype(np.uint8)