How to Convert Images to Black and White Grayscale using Python
Black and white images offer a desirable, moody aesthetic with greater detail in shadowy areas, and they also boast a smaller file size than their color-filled counterparts. Conveniently, calling the below API in your Python workflow will allow you to quickly convert color images to black and white grayscale through programmatic color removal. It’s free and easy to use following steps below.
First run this command to install the Python SDK:
pip install cloudmersive-image-api-client
Then add the imports and call the 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
# Configure API key authorization: Apikey
configuration = cloudmersive_image_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_image_api_client.FilterApi(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:
# Convert image to black-and-white grayscale
api_response = api_instance.filter_black_and_white(image_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling FilterApi->filter_black_and_white: %s\n" % e)
Now simply pass a free-tier API key in your request to complete the call, and you’re ready to go. Your only input request parameter is the image file path, so it’s nice and simple to use.