How to add a Gaussian blur to an image in Python

Let’s cut right to the chase: here’s the absolute easiest way to create a Gaussian blur effect using Python.

Install our API client with pip install, the command for which is shown here:

pip install cloudmersive-image-api-client

We should write out our function call next and specify its parameters:

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.FilterApi(cloudmersive_image_api_client.ApiClient(configuration))radius = 56 # int | Radius in pixels of the blur operation; a larger radius will produce a greater blur effectsigma = 56 # int | Sigma, or variance, of the gaussian blur operationimage_file = '/path/to/file' # file | Image file to perform the operation on.  Common file formats such as PNG, JPEG are supported.try:# Perform a guassian blur on the input imageapi_response = api_instance.filter_gaussian_blur(radius, sigma, image_file)pprint(api_response)except ApiException as e:print("Exception when calling FilterApi->filter_gaussian_blur: %s\n" % e)

Now we need to provide filter_gaussian_blur with an image, radius, and sigma to create our effect. That’s really all there is to it; the API will created the blurred image and then return a URL for its download location. Let’s give it a whirl on this crisp image here:

Image for post

Our results look like this with a radius of 10 and sigma of 5:

Image for post

Softened up quite a bit.

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