How to zoom in on a certain part of an image using PIL?

时间:2019-01-15 18:11:26

标签: python python-imaging-library

I used a module called face recognition to create an outline of the lips on any photo, and wanted to modify the picture to just see the lips and not the full face. However I don't know how to do this.

I have tried to convert the face_landmarks into a numpy array and then show it, but realized that it just shows the coordinates of the lips.

from PIL import Image, ImageDraw
import face_recognition
import numpy as np

# Load the jpg file into a numpy array
image = 

face_recognition.load_image_file("/Users/23Athreyad/Documents/trump.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))
    d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))


   print(face_landmarks['top_lip'])
   pil_image.show()

The expected result is a zoomed in picture of the lips, but I am not sure how to get to that.

1 个答案:

答案 0 :(得分:0)

如果要提取定界嘴唇的矩形,则只需获取矩形左上角的坐标(最小的“ x”和“ y”坐标)和右下角的坐标(最大的“ x”)和“ y”坐标)并使用:

<1/15>

请注意,lip = image.crop((min_x, min_y, max_x, max_y)) image对象。

相关问题