Spaces:
Running
Running
Commit
·
be24af1
1
Parent(s):
3d5d69a
- filters.py +3 -17
filters.py
CHANGED
@@ -116,30 +116,17 @@ def pixelize(image, pixel_size: int = 10):
|
|
116 |
return pixelized_image
|
117 |
|
118 |
|
119 |
-
@registry.register("Sketch Effect"
|
120 |
-
|
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)
|
@@ -150,8 +137,7 @@ def sketch_effect(image, blur_kernel_size: int = 21):
|
|
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)
|
|
|
116 |
return pixelized_image
|
117 |
|
118 |
|
119 |
+
@registry.register("Sketch Effect")
|
120 |
+
def sketch_effect(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
"""
|
122 |
## Apply a sketch effect to the image.
|
123 |
|
124 |
**Args:**
|
125 |
* `image` (numpy.ndarray): Input image (BGR or grayscale)
|
|
|
126 |
|
127 |
**Returns:**
|
128 |
* `numpy.ndarray`: Sketch effect applied image
|
129 |
"""
|
|
|
|
|
|
|
|
|
130 |
# Convert the image to grayscale
|
131 |
if len(image.shape) == 3:
|
132 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
137 |
inverted_gray = cv2.bitwise_not(gray)
|
138 |
|
139 |
# Apply Gaussian blur to the inverted image
|
140 |
+
blurred = cv2.GaussianBlur(inverted_gray, (21, 21), 0) # Fixed kernel size
|
|
|
141 |
|
142 |
# Blend the grayscale image with the blurred inverted image
|
143 |
sketch = cv2.divide(gray, 255 - blurred, scale=256)
|