eienmojiki commited on
Commit
3d5d69a
·
1 Parent(s): ad0c787

Refactor filters.py to add sketch effect and improve pixelization

Browse files

- Added sketch effect filter
- Updated pixelize function formatting
- Improved dot effect filter spacing
- Ensured odd kernel size for blur
- Cleaned up code formatting and comments

Files changed (1) hide show
  1. filters.py +36 -18
filters.py CHANGED
@@ -2,10 +2,12 @@ import cv2
2
  import numpy as np
3
  from registry import registry
4
 
 
5
  @registry.register("Original")
6
  def original(image):
7
  return image
8
 
 
9
  @registry.register("Dot Effect", defaults={
10
  "dot_size": 10,
11
  "dot_spacing": 2,
@@ -78,6 +80,7 @@ def dot_effect(image, dot_size: int = 10, dot_spacing: int = 2, invert: bool = F
78
 
79
  return canvas
80
 
 
81
  @registry.register("Pixelize", defaults={
82
  "pixel_size": 10,
83
  }, min_vals={
@@ -103,39 +106,54 @@ def pixelize(image, pixel_size: int = 10):
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
112
 
113
- @registry.register("Motion Blur", defaults={
114
- "kernel_size": 10,
 
115
  }, min_vals={
116
- "kernel_size": 1,
117
  }, max_vals={
118
- "kernel_size": 50,
119
  }, step_vals={
120
- "kernel_size": 1,
121
  })
122
- def motion_blur(image, kernel_size: int = 10):
123
  """
124
- ## Apply a motion blur effect to the image.
125
 
126
  **Args:**
127
  * `image` (numpy.ndarray): Input image (BGR or grayscale)
128
- * `kernel_size` (int): Size of the kernel
129
 
130
  **Returns:**
131
- * `numpy.ndarray`: Motion blurred image
132
  """
133
- # Create a horizontal kernel
134
- kernel = np.zeros((kernel_size, kernel_size))
135
- kernel[kernel_size//2, :] = np.ones(kernel_size)
136
- kernel = kernel / kernel_size
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
- # Apply the kernel to the image
139
- motion_blurred_image = cv2.filter2D(image, -1, kernel)
140
 
141
- return motion_blurred_image
 
2
  import numpy as np
3
  from registry import registry
4
 
5
+
6
  @registry.register("Original")
7
  def original(image):
8
  return image
9
 
10
+
11
  @registry.register("Dot Effect", defaults={
12
  "dot_size": 10,
13
  "dot_spacing": 2,
 
80
 
81
  return canvas
82
 
83
+
84
  @registry.register("Pixelize", defaults={
85
  "pixel_size": 10,
86
  }, min_vals={
 
106
  # Resize the image to a smaller size
107
  small_height = height // pixel_size
108
  small_width = width // pixel_size
109
+ small_image = cv2.resize(
110
+ image, (small_width, small_height), interpolation=cv2.INTER_LINEAR)
111
 
112
  # Resize back to the original size with nearest neighbor interpolation
113
+ pixelized_image = cv2.resize(
114
+ small_image, (width, height), interpolation=cv2.INTER_NEAREST)
115
 
116
  return pixelized_image
117
 
118
+
119
+ @registry.register("Sketch Effect", defaults={
120
+ "blur_kernel_size": 21,
121
  }, min_vals={
122
+ "blur_kernel_size": 1,
123
  }, max_vals={
124
+ "blur_kernel_size": 51,
125
  }, step_vals={
126
+ "blur_kernel_size": 2,
127
  })
128
+ def sketch_effect(image, blur_kernel_size: int = 21):
129
  """
130
+ ## Apply a sketch effect to the image.
131
 
132
  **Args:**
133
  * `image` (numpy.ndarray): Input image (BGR or grayscale)
134
+ * `blur_kernel_size` (int): Size of the Gaussian blur kernel (must be odd)
135
 
136
  **Returns:**
137
+ * `numpy.ndarray`: Sketch effect applied image
138
  """
139
+ # Ensure the kernel size is odd
140
+ if blur_kernel_size % 2 == 0:
141
+ blur_kernel_size += 1
142
+
143
+ # Convert the image to grayscale
144
+ if len(image.shape) == 3:
145
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
146
+ else:
147
+ gray = image
148
+
149
+ # Invert the grayscale image
150
+ inverted_gray = cv2.bitwise_not(gray)
151
+
152
+ # Apply Gaussian blur to the inverted image
153
+ blurred = cv2.GaussianBlur(
154
+ inverted_gray, (blur_kernel_size, blur_kernel_size), 0)
155
 
156
+ # Blend the grayscale image with the blurred inverted image
157
+ sketch = cv2.divide(gray, 255 - blurred, scale=256)
158
 
159
+ return sketch