Crop Image to Face with Round Crop in Python

Cloudmersive
2 min readFeb 9, 2022

--

In a prior article, we highlighted an iteration of the Cloudmersive Image API that can recognize a face within an image & crop the image to a small square section around that face. Equally useful to this iteration is one (/image/face/crop/first/round) which performs the same process resulting in a circular image around the desired face. It’s very useful to have this capability in your application or flow for the purpose of creating ID photos or profile pictures for various online accounts. Below, we’ll walk through connecting to this API in Python with code copied from the Cloudmersive API console.

The first step is to install the Python SDK using the command below:

pip install cloudmersive-image-api-client

After that, you can start off the API call function with the below code:

from __future__ import print_function
import time
import cloudmersive_image_api_client
from cloudmersive_image_api_client.rest import ApiException
from pprint import pprint

Next, you can authenticate your Cloudmersive API key and complete the callback function using the remaining snippets below:

# Configure API key authorization: Apikey
configuration = cloudmersive_image_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_image_api_client.FaceApi(cloudmersive_image_api_client.ApiClient(configuration))
image_file = '/path/to/inputfile' # file | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try:
# Crop image to face with round crop
api_response = api_instance.face_crop_first_round(image_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling FaceApi->face_crop_first_round: %s\n" % e)

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet