Invert, Negate the Colors in an Image in Python

Cloudmersive
1 min readFeb 7, 2022

Let’s get contrast-y. Inverting the colors of an image is easy with the Cloudmersive Image API, using the image/edit/remove-transparency iteration. Simply copy & paste the below code into your Python development environment and reap the benefits.

To get started, run this command & install the Python SDK:

pip install cloudmersive-image-api-client

Next, begin the API call function using the below code & be sure to authenticate your Cloudmersive API key where directed:

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'

Then, wrap up the function with the final code snippet below:

# create an instance of the API class
api_instance = cloudmersive_image_api_client.EditApi(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:
# Invert, negate the colors in the image
api_response = api_instance.edit_invert(image_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling EditApi->edit_invert: %s\n" % e)

--

--

Cloudmersive

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