From ad38eef03bdad1b4d4ed786933068a90bc3d2d96 Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Thu, 24 Apr 2025 11:46:45 +0200 Subject: [PATCH] feat: added proper pixelation --- secure_pixelation/pixelation_process.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/secure_pixelation/pixelation_process.py b/secure_pixelation/pixelation_process.py index 1bcc4a3..fd7ff12 100644 --- a/secure_pixelation/pixelation_process.py +++ b/secure_pixelation/pixelation_process.py @@ -31,6 +31,22 @@ def impaint(raw_image: RawImage, image: Optional[np.ndarray] = None) -> np.ndarr return cv2.inpaint(image, mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA) + +def pixelate_regions(raw_image: RawImage, image: Optional[np.ndarray] = None, pixel_size: int = 10) -> np.ndarray: + image = image.copy() if image is not None else raw_image.get_image().copy() + + for (x, y, w, h) in raw_image.bounding_boxes: + roi = image[y:y+h, x:x+w] + + # Resize down and then back up + temp = cv2.resize(roi, (max(1, w // pixel_size), max(1, h // pixel_size)), interpolation=cv2.INTER_LINEAR) + pixelated = cv2.resize(temp, (w, h), interpolation=cv2.INTER_NEAREST) + + image[y:y+h, x:x+w] = pixelated + + return image + + def pixelate(to_detect: str, debug_drawings: bool = True): raw_image = RawImage(to_detect) @@ -46,8 +62,13 @@ def pixelate(to_detect: str, debug_drawings: bool = True): cv2.imwrite(f, image) + write_image(raw_image.image, "step_0") + step_1 = blackout(raw_image) write_image(step_1, "step_1") step_2 = impaint(raw_image, image=step_1) write_image(step_2, "step_2") + + step_3 = pixelate_regions(raw_image, image=step_2) + write_image(step_3, "step_3")