How to adaptively adjust the contrast of an image in Python

A great man once said, “With great image quantities comes great blandness!” Well, maybe not that exactly. But we do need a method for jazzing up images efficiently and automatically, whether it’s for a photo app, website, etc. Enter: today’s API, which was created to adaptively change image contrast to make almost any image look more appealing.

Let’s start out by running this command for pip install.

pip install cloudmersive-image-api-client

Now that our API client is accessible, let’s use it to call edit_contrast_adaptive, as you see in this code block here:

from __future__ import print_functionimport timeimport cloudmersive_image_api_clientfrom import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_image_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_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/file' # 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 seeapi_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)

This will require an image file, of course, as well as a gamma value, which you can experiment with for the desired effect. And there you have it, our complete setup for adaptive contrast!

Image for post
Image for post

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