How to crop an image to the face in Python

In two easy steps, we will learn how to use an API to crop to the face in an image.

First, install the API using pip installer, like so:

pip install git+

Next we have a choice between a round or square crop by calling face_crop_first_round or face_crop_first, respectively. In this example we will be using round cropping:

from __future__ import print_function
import time
import cloudmersive_image_api_client
from import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_image_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['Apikey'] = 'Bearer'
# create an instance of the API class
api_instance = cloudmersive_image_api_client.FaceApi(cloudmersive_image_api_client.ApiClient(configuration))
image_file = '/path/to/file' # file | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
# Crop image to face (round)
api_response = api_instance.face_crop_first_round(image_file)
except ApiException as e:
print("Exception when calling FaceApi->face_crop_first_round: %s\n" % e)

And it’s really that easy! Here is a quick example:

Image for post

And the resulting cropped image:

Image for post

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store