使用 PIL 裁剪和保存面部特征

Crop and save facial landmarks using PIL

我想裁剪上唇和下唇并将其保存到一张图片中。 我正在使用这个 GitHub 库 face_recognition

这是代码:

from PIL import Image, ImageDraw
import face_recognition

# Load the jpg file into a numpy array
image = face_recognition.load_image_file("me.jpg")

# Find all facial features in all the faces in the image
face_landmarks_list = face_recognition.face_landmarks(image)


for face_landmarks in face_landmarks_list:
    pil_image = Image.fromarray(image)
    d = ImageDraw.Draw(pil_image, 'RGBA')


    # Gloss the lips
    d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128), outline=None)
    d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128), outline=None)
    d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)
    d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)


    # cropped_top_lip = image.crop(face_landmarks['top_lip'])
    # cropped_top_lip.save('top_lip.jpg')
    # cropped_bottom_lip = image.crop(face_landmarks['bottom_lip'])
    # cropped_bottom_lip.save('bottom_lip.jpg')

    pil_image.save('me2.jpg')

这张return全图,我只想要嘴唇部分。

这是 face_landmarks['top_lip'] 的打印:

[(498, 937), (546, 926), (597, 924), (637, 930), (676, 922), (726, 922), (772, 929), (756, 935), (677, 940), (637, 946), (597, 942), (516, 942)]

.crop 方法采用包含 4 个裁剪区域坐标的列表 -

[
    X_top_left_corner, Y_top_left_corner, 
    X_bottom_right_corner, Y_bottom_right_corner
] 

face_landmarks['top_lip']的坐标可以看出,我想,这些是嘴唇轮廓上的点。因此,您需要找到最左上角和最右下角,并将其传递给 crop 方法。

[(498, 937), (546, 926), (597, 924), (637, 930), (676, 922), (726, 922), (772, 929), (756, 935), (677, 940), (637, 946), (597, 942), (516, 942)]

要找到左上角,您需要从该数据中找到最小的 X 坐标和最小的 Y 坐标。并且这两个坐标可以存在于单独的元组中。

对于右下角,您需要最高的 X 坐标和最高的 y 坐标。同样,这两个可以存在于单独的元组中。


你可以这样确定角点:

x1 = min(my_list)[0] # top left corner X
y1 = min(my_list, key=lambda pair: pair[1])[1] # top left corner Y

x2 = max(my_list)[0] # bottom right corner X
y2 = max(my_list, key=lambda pair: pair[1])[1] # bottom right corner Y

在这种情况下,最小的 X 坐标是 498,最小的 Y 坐标是 922。这些将成为您的左上角。

最高 X 坐标为 772,最高 Y 坐标为 946。这些将是您的右下角。

这应该适用于裁剪上唇:

# im.crop([x1, y1, x2, y2])
im.crop([498, 922, 772, 946])