How to add a drop shadow to an image in Python

Whether an app feature or a part of your photo upload flow, drop shadows add easy visual interest to virtually any image. Setting up an automated system to handle them is a chore, however. To save us from this irritation, an API can be used that lets us create customizable drop shadows in a couple simple steps.

Let’s install the client first:

pip install cloudmersive-image-api-client

Next we should set up a function call for edit_drop_shadow and enter our parameters for the offset, sigma, and opacity.

from __future__ import print_functionimport timeimport cloudmersive_image_api_clientfrom cloudmersive_image_api_client.rest 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))x = 56 # int | Horizontal (X) offset of the drop shadowy = 56 # int | Vertical (Y) offset of the drop shadowsigma = 56 # int | Sigma (blur distance) of the drop shadowopacity = 56 # int | Opacity of the drop shadow; 0 is 0% and 100 is 100%image_file = '/path/to/file' # file | Image file to perform the operation on.  Common file formats such as PNG, JPEG are supported.try:# Add a customizeable drop shadow to an imageapi_response = api_instance.edit_drop_shadow(x, y, sigma, opacity, image_file)pprint(api_response)except ApiException as e:print("Exception when calling EditApi->edit_drop_shadow: %s\n" % e)

Drop in an image and voila, your drop shadow is complete! Here’s what it looks like using 15, 25, 5, and 80 for horizontal and vertical offset, sigma, and opacity, respectively.

Image for post
Image for post

Written by

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