Skip to content

Instantly share code, notes, and snippets.

@Pelirrojo
Created March 13, 2024 23:15
Show Gist options
  • Save Pelirrojo/2ab353ac1c26fcf713a6492313898c69 to your computer and use it in GitHub Desktop.
Save Pelirrojo/2ab353ac1c26fcf713a6492313898c69 to your computer and use it in GitHub Desktop.
async def generate_blurred(
image_path: str,
output_path: str,
original_filename: str,
original_extension: str,
faces_detail: dict,
ids_requested: dict,
) -> str:
image = Image.open(image_path)
imgWidth, imgHeight = image.size
for id_request in ids_requested:
# draw = ImageDraw.Draw(image)
box = faces_detail[id_request]
left = imgWidth * box["Left"]
top = imgHeight * box["Top"]
width = imgWidth * box["Width"]
height = imgHeight * box["Height"]
# Create rounded rectangle mask
mask = Image.new("L", image.size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse([left, top, left + width, top + height], fill=255)
# Blur image
blurred = image.filter(ImageFilter.GaussianBlur(20))
# Paste blurred region and save result
image.paste(blurred, mask=mask)
new_file_name = f"{output_path}/{original_filename}-blurried.{original_extension}"
img_with_border = ImageOps.expand(image, border=border_size, fill=border_fill)
img_with_border.save(new_file_name)
return new_file_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment