Photo-Filter-2.0 / filters.py
ngd1210's picture
Added filter documentation and UI updates
fd32459
raw
history blame
2.32 kB
import cv2
import numpy as np
from registry import registry
@registry.register("Original")
def 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 your image into a dotted pattern.**
__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 the dots
__Returns:__
numpy.ndarray: Dotted image
"""
# Convert to grayscale if image is color
if len(image.shape) == 3:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
gray = image
# Apply adaptive thresholding to improve contrast
gray = cv2.adaptiveThreshold(
gray,
255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,
25, # Block size
5 # Constant subtracted from mean
)
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)
# Dynamic dot sizing based on brightness
relative_brightness = brightness / 255.0
if invert:
relative_brightness = 1 - relative_brightness
# Draw circle with size proportional to 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