How to Posterize an Image using Python
Year over year, handheld and smartphone cameras seem to receive constant improvements enabling us to take increasingly detailed images.
However, detail isn’t always necessary for a desirable photo aesthetic. Through image posterization, you can reduce the number of distinct colors in your image to create a smooth and interesting effect, emulating a look one might associate with decades-old movie or political posters.
The free-to-use API provided below makes it easy to posterize images in your Python workflow. You can use complementary, ready-to-run code examples to structure your API call, and you can complete your request by defining the levels (number of unique colors to retain in the output image) and your image file path.
Let’s begin by running this command to install the Python SDK:
pip install cloudmersive-image-api-client
And then let’s copy in the imports & call the function:
from __future__ import print_function
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))
levels = 56 # int | Number of unique colors to retain in the output image
image_file = '/path/to/inputfile' # file | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
# Posterize the image by reducing distinct colors
api_response = api_instance.filter_posterize(levels, image_file)
except ApiException as e:
print("Exception when calling FilterApi->filter_posterize: %s\n" % e)
To complete your API call, you just need to include a free-tier API key in your request, which you can get by registering a free account here. That’s all there is to it!