import os from typing import List from PIL import Image, ImageDraw, ImageFont, ImageFilter from BlurMe.ml.element_detection import BoundBox DIR_PATH = os.path.dirname(os.path.realpath(__file__)) def show_image_with_boxes( in_image_path: str, bounding_boxes: List[BoundBox], out_image_path: str = None ): img = Image.open(in_image_path) draw = ImageDraw.Draw(img) font_path = DIR_PATH + "/assets/fonts/arial.ttf" font = ImageFont.truetype(font_path, 25) for i, box in enumerate(bounding_boxes): draw.rectangle(box.get_params(), outline="red", width=2, fill=None) draw.text((box.x1 + 5, box.y1 + 5), str(i + 1), fill="red", font=font) if not out_image_path: out_image_path = ( in_image_path.split(".")[0] + "_out." + in_image_path.split(".")[1] ) img.save(out_image_path) def blur_image_with_boxes( in_image_path: str, bounding_boxes: List[BoundBox], out_image_path: str = None ): img = Image.open(in_image_path) for box in bounding_boxes: if box.selected: cropped = img.crop(box.get_params()) blurred = cropped.filter(ImageFilter.GaussianBlur(radius=10)) img.paste(blurred, box.get_params()) if not out_image_path: out_image_path = ( in_image_path.split(".")[0] + "_blurred." + in_image_path.split(".")[1] ) img.save(out_image_path)