Rotate an Image in Python

Cloudmersive
2 min readFeb 4, 2022

Setting up your very own image editing app or automated flow? The Cloudmersive Image API can be a big help, providing important photo-editing services among its suite of useful iterations. Using the Rotate Image iteration of the Image API (/image/edit/rotate/{degrees}/angle), you can rotate an image any number of degrees (values range from 0.0 to 360.0) in a few simple steps. Connecting is easy, with ready-to-run code sourced from the Cloudmersive API console — let’s look at how it’s done in Python.

You can start off by running the below ‘pip install’ command, which will install the Python SDK, and then copy the remaining code to begin the callback function:

pip install cloudmersive-image-api-clientfrom __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 using the below code snippet:

# Configure API key authorization: Apikey
configuration = cloudmersive_image_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'

After that, you can copy the below code to complete the callback function. You’ll need to specify the number of degrees you want the image to rotate at this stage:

# create an instance of the API class
api_instance = cloudmersive_image_api_client.EditApi(cloudmersive_image_api_client.ApiClient(configuration))
degrees = 1.2 # float | Degrees to rotate the image; values range from 0.0 to 360.0.
image_file = '/path/to/inputfile' # file | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try:
# Rotate an image any number of degrees
api_response = api_instance.edit_rotate(degrees, image_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling EditApi->edit_rotate: %s\n" % e)

--

--

Cloudmersive

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