Crop an Image to a Circular Area in Python
In a previous article, we walked through how you can crop rectangular areas of an input image using an ‘Edit’ iteration of the Cloudmersive Image API. Now, we’ll go through a similar ‘Edit’ iteration which you can use to crop your image into a circular area. Doing this requires a commonly supported image file format (e.g. PNG, JPG, etc.) and specific integers determining the top & left areas and new radius of the cropped photo. You can integrate this API in over 13 common programming languages with code from the Cloudmersive API console page — let’s see how it’s done in Python.
To begin with, run the Python SDK installation command:
pip install cloudmersive-image-api-client
Next, kickoff the callback function with the below code, and input your Cloudmersive API key in the latter snippet where prompted:
from __future__ import print_function
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, complete the function using the below code snippet & await your cropped image:
# create an instance of the API class
api_instance = cloudmersive_image_api_client.EditApi(cloudmersive_image_api_client.ApiClient(configuration))
left = 56 # int | The left edge of the circular crop area in pixels (X).
top = 56 # int | The top edge of the circular crop area in pixels (Y).
radius = 56 # int | The radius of the circular crop area in pixels.
image_file = '/path/to/inputfile' # file | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.try:
# Crop an image to an circular area
api_response = api_instance.edit_crop_circle(left, top, radius, image_file)
except ApiException as e:
print("Exception when calling EditApi->edit_crop_circle: %s\n" % e)