Crop Image to Face with Square Crop in Python

Cloudmersive
2 min readFeb 9, 2022

--

The best photos of us are often overwhelmed by our surroundings when the photo was taken. Especially given the frequency with which we use headshot photographs for profile/identification purposes, it’s really useful to be able to reduce any given photo to a smaller headshot of our face (or the faces of others). With the /image/face/crop/first iteration of the Cloudmersive Image API, you can accomplish this cropping in a few simple steps. Let’s walk through using this API in Python.

You can begin by running the Python installation SDK:

pip install cloudmersive-image-api-client

After that, you can kickoff the callback function & authenticate your Cloudmersive API key for access:

from __future__ import print_function
import time
import cloudmersive_image_api_client
from cloudmersive_image_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_image_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'

Finally, use the below code to wrap up the callback function, and you’re ready to start cropping:

# 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 square crop
api_response = api_instance.face_crop_first(image_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling FaceApi->face_crop_first: %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