Adaptively Adjust the Contrast of an Image in Python

The Cloudmersive Image API includes over a dozen iterations that can help with editing images in various capacities. In this article, we’ll walk through connecting to one such useful iteration in Python. This iteration uses Gamma to adaptively adjust the contrast for an input image (/image/edit/contrast/{gamma}/adaptive), improving the viewability and visual appeal of the image.

To use this API, all you need to do is copy & paste the below ready-to-run code from the Cloudmersive API console into your environment. You can start by running the below command to install the Python SDK:

pip install cloudmersive-image-api-client

Next, you can begin the callback function:

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

After that you’ll need to copy in the API key authentication code snippet below. If you don’t have a key, visit Cloudmersive.com and make a free account to receive one.

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

Finally, you can finish the callback function with the below code:

# create an instance of the API class
api_instance = cloudmersive_image_api_client.EditApi(cloudmersive_image_api_client.ApiClient(configuration))
gamma = 1.2 # float | Gamma value to adjust the contrast in the image. Recommended value is 2.0. Values between 0.0 and 1.0 will reduce contrast, while values above 1.0 will increase contrast.
image_file = '/path/to/inputfile' # file | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try:
# Adaptively adjust the contrast of the image to be more appealing and easy to see
api_response = api_instance.edit_contrast_adaptive(gamma, image_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling EditApi->edit_contrast_adaptive: %s\n" % e)

--

--

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
Cloudmersive

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