Created
June 25, 2021 03:40
-
-
Save yusanshi/56f3f56706e88632cc2d196972fbbc88 to your computer and use it in GitHub Desktop.
Add mosaic to group photos.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import face_recognition | |
import argparse | |
from PIL import Image, ImageDraw, ImageFont | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--input_path', type=str, required=True) | |
parser.add_argument('--mosaic_path', type=str, required=True) | |
parser.add_argument('--output_path', type=str, default='./output.jpg') | |
parser.add_argument('--zoom_coefficient', type=float, default=2.0) | |
parser.add_argument('--font_size', type=int, default=120) | |
args = parser.parse_args() | |
image = face_recognition.load_image_file(args.input_path) | |
face_locations = face_recognition.face_locations(image) | |
image = Image.fromarray(image) | |
mosaic = Image.open(args.mosaic_path).convert('RGBA') | |
temp_image = image.copy() | |
for index, location in enumerate(face_locations): | |
location_width = location[1] - location[3] | |
location_height = location[2] - location[0] | |
center_x = int((location[1] + location[3]) / 2) | |
center_y = int((location[0] + location[2]) / 2) | |
ratio = min(mosaic.width / location_width, mosaic.height / location_height) | |
ratio = ratio / args.zoom_coefficient | |
temp_mosaic = mosaic.resize( | |
(int(mosaic.width / ratio), int(mosaic.height / ratio))) | |
drawing_location = ( | |
int(center_x - temp_mosaic.width / 2), | |
int(center_y - temp_mosaic.height / 2), | |
) | |
temp_image.paste(temp_mosaic, drawing_location, temp_mosaic) | |
font = ImageFont.truetype("arial.ttf", args.font_size) | |
ImageDraw.Draw(temp_image).text(drawing_location, | |
str(index), | |
fill='red', | |
font=font) | |
# Make sure displayed image in MAX range | |
MAX_DISPLAY_WIDTH = 2000 | |
MAX_DISPLAY_HEIGHT = 1000 | |
ratio = max(temp_image.width / MAX_DISPLAY_WIDTH, | |
temp_image.height / MAX_DISPLAY_HEIGHT) | |
temp_image.resize( | |
(int(temp_image.width / ratio), int(temp_image.height / ratio))).show() | |
deleting = input('Input indexs to delete, seperated with space:\n') | |
deleting = [int(x.strip()) for x in deleting.split() if x.strip() != ''] | |
for location in [x for i, x in enumerate(face_locations) if i not in deleting]: | |
location_width = location[1] - location[3] | |
location_height = location[2] - location[0] | |
center_x = int((location[1] + location[3]) / 2) | |
center_y = int((location[0] + location[2]) / 2) | |
ratio = min(mosaic.width / location_width, mosaic.height / location_height) | |
ratio = ratio / args.zoom_coefficient | |
temp_mosaic = mosaic.resize( | |
(int(mosaic.width / ratio), int(mosaic.height / ratio))) | |
drawing_location = ( | |
int(center_x - temp_mosaic.width / 2), | |
int(center_y - temp_mosaic.height / 2), | |
) | |
image.paste(temp_mosaic, drawing_location, temp_mosaic) | |
image.save(args.output_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment