【问题标题】:Crop and save facial landmarks using PIL使用 PIL 裁剪和保存面部标志
【发布时间】:2019-04-21 13:36:50
【问题描述】:

我想裁剪上唇和下唇并将其保存到一张图像中。 我正在使用这个 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')

这会返回完整的图像,我只想要嘴唇部分。

这是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)]

【问题讨论】:

    标签: python image-processing face-recognition


    【解决方案1】:

    .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])
    

    【讨论】:

      猜你喜欢
      • 2018-03-24
      • 2015-08-25
      • 2011-09-21
      • 1970-01-01
      • 2012-10-01
      • 2012-04-16
      • 1970-01-01
      • 2011-04-19
      • 2019-06-10
      相关资源
      最近更新 更多