eienmojiki commited on
Commit
41bdd67
·
1 Parent(s): 8e75d7b

Added pixelize filter and reinitialized controls

Browse files

- Introduced pixelize filter effect
- Reinitialized filter controls
- Updated dot_effect indentation
- Added pixel_size parameter defaults
- Improved image resizing logic

Files changed (2) hide show
  1. app.py +4 -0
  2. filters.py +47 -15
app.py CHANGED
@@ -13,6 +13,10 @@ def create_app():
13
  controls = create_filter_controls()
14
  filter_names = list(registry.filters.keys())
15
 
 
 
 
 
16
  filter_groups = {} # Store filter groups
17
 
18
  with gr.Row():
 
13
  controls = create_filter_controls()
14
  filter_names = list(registry.filters.keys())
15
 
16
+ # Re-initialize controls and filter_names after adding the new filter
17
+ controls = create_filter_controls()
18
+ filter_names = list(registry.filters.keys())
19
+
20
  filter_groups = {} # Store filter groups
21
 
22
  with gr.Row():
filters.py CHANGED
@@ -62,18 +62,50 @@ def dot_effect(image, dot_size: int = 10, dot_spacing: int = 2, invert: bool = F
62
  if region.size > 0:
63
  brightness = np.mean(region)
64
 
65
- # Dynamic dot sizing based on brightness
66
- relative_brightness = brightness / 255.0
67
- if invert:
68
- relative_brightness = 1 - relative_brightness
69
-
70
- # Draw circle with size proportional to brightness
71
- radius = int((dot_size/2) * relative_brightness)
72
- if radius > 0:
73
- cv2.circle(canvas,
74
- (x + dot_size//2, y + dot_size//2),
75
- radius,
76
- (dot_color),
77
- -1)
78
-
79
- return canvas
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  if region.size > 0:
63
  brightness = np.mean(region)
64
 
65
+ # Dynamic dot sizing based on brightness
66
+ relative_brightness = brightness / 255.0
67
+ if invert:
68
+ relative_brightness = 1 - relative_brightness
69
+
70
+ # Draw circle with size proportional to brightness
71
+ radius = int((dot_size/2) * relative_brightness)
72
+ if radius > 0:
73
+ cv2.circle(canvas,
74
+ (x + dot_size//2, y + dot_size//2),
75
+ radius,
76
+ (dot_color),
77
+ -1)
78
+
79
+ return canvas
80
+
81
+ @registry.register("Pixelize", defaults={
82
+ "pixel_size": 10,
83
+ }, min_vals={
84
+ "pixel_size": 1,
85
+ }, max_vals={
86
+ "pixel_size": 50,
87
+ }, step_vals={
88
+ "pixel_size": 1,
89
+ })
90
+ def pixelize(image, pixel_size: int = 10):
91
+ """
92
+ ## Apply a pixelization effect to the image.
93
+
94
+ **Args:**
95
+ * `image` (numpy.ndarray): Input image (BGR or grayscale)
96
+ * `pixel_size` (int): Size of each pixel block
97
+
98
+ **Returns:**
99
+ * `numpy.ndarray`: Pixelized image
100
+ """
101
+ height, width = image.shape[:2]
102
+
103
+ # Resize the image to a smaller size
104
+ small_height = height // pixel_size
105
+ small_width = width // pixel_size
106
+ small_image = cv2.resize(image, (small_width, small_height), interpolation=cv2.INTER_LINEAR)
107
+
108
+ # Resize back to the original size with nearest neighbor interpolation
109
+ pixelized_image = cv2.resize(small_image, (width, height), interpolation=cv2.INTER_NEAREST)
110
+
111
+ return pixelized_image