BLUR-39: Add simple method of blurring.

This commit is contained in:
Gosia 2023-12-19 10:55:25 +01:00
parent e990582cbd
commit b27f4930db

View File

@ -1,8 +1,8 @@
import os import os
from typing import List from typing import List
from PIL import Image, ImageDraw, ImageFont, ImageFilter from PIL import Image, ImageDraw, ImageFont, ImageFilter
from PIL.Image import composite
from BlurMe.ml.element_detection import BoundBox from BlurMe.ml.element_detection import BoundBox
@ -26,15 +26,21 @@ def show_image_with_boxes(
img.save(out_image_path) img.save(out_image_path)
def blur_image_with_boxes( def blur_boxes_on_image(
in_image_path: str, bounding_boxes: List[BoundBox], out_image_path: str = None in_image_path: str, bounding_boxes: List[BoundBox], out_image_path: str = None
): ):
img = Image.open(in_image_path) img = Image.open(in_image_path)
blur_img = img.filter(ImageFilter.GaussianBlur(radius=7))
mask = Image.new("L", img.size, 255)
draw_mask = ImageDraw.Draw(mask)
for box in bounding_boxes: for box in bounding_boxes:
if box.selected: if box.selected:
cropped = img.crop(box.get_params()) if box.object == "plate":
blurred = cropped.filter(ImageFilter.GaussianBlur(radius=10)) draw_mask.rectangle(box.get_params(), fill=0)
img.paste(blurred, box.get_params()) elif box.object == "face":
draw_mask.rectangle(box.get_params(), fill=0)
mask = mask.filter(ImageFilter.GaussianBlur(radius=3))
img = composite(img, blur_img, mask)
if not out_image_path: if not out_image_path:
out_image_path = ( out_image_path = (
in_image_path.split(".")[0] + "_blurred." + in_image_path.split(".")[1] in_image_path.split(".")[0] + "_blurred." + in_image_path.split(".")[1]