Python Pillow - Embossing Images



In general, embossing used for creating raised relief images on paper or cardstock to achieve a three-dimensional appearance, was notably used by the British postal service during the 19th century to add an elegant touch to stamps. This process involves raising certain parts of the image, design, or text to create a three-dimensional effect.

In image processing and computer graphics, image embossing is a technique where each pixel in an image is transformed into either a highlighted or shadowed version, depending on the original image's light and dark boundaries. Areas with low contrast are replaced by a gray background. The resulting embossed image effectively represents the rate of color change at each location within the original image. Applying an embossing filter to an image can often produce an output resembling a paper or metal-embossed rendition of the original image, hence its name.

Python's Pillow library offers several standard image filters within the ImageFilter module to perform the different filter operations on the image by calling the image.filter() method. In this tutorial, we will see the working of the embossing filter provided by the ImageFilter module.

Applying ImageFilter.EMBOSS kernel filter with the Image.filter() method

The ImageFilter.EMBOSS filter is one of the built-in filter options available in the current version of the Pillow library and is used to create an embossed effect in the image.

Following are the steps for applying image embossing −

  • Load the input image using the Image.open() function.

  • Apply the filter() function to the loaded Image object and provide ImageFilter.EMBOSS as an argument to the function. The function will return the embossed image as a PIL.Image.Image object.

Example

Here is an example using the Image.filter() method with ImageFilter.EMBOSS kernel filter.

from PIL import Image, ImageFilter

# Load an input image
input_image = Image.open("Images/TP logo.jpg")

# Apply an emboss effect to the image
embossed_result = input_image.filter(ImageFilter.EMBOSS)

# Display the input image
input_image.show()

# Display the modified image with the emboss effect
embossed_result.show()

Input Image

tp logo

Output Image

Output filtered image with emboss effect −

imagefilter emboss

Customizing the EMBOSS filter to add depth and azimuth to the embossed image

In the source code of the pillow library, we can observe the EMBOSS class in the "ImageFilter.py," module. This class represents the embossing filter and provides a basic way to create an embossed effect on an image.

# Embossing filter.
class EMBOSS(BuiltinFilter):
   name = "Emboss"
   # fmt: off
   filterargs = (3, 3), 1, 128, (
      -1, 0, 0,
      0,  1, 0,
      0,  0, 0,
   )

The filter operates by applying a matrix to the image pixel by pixel. The matrix contains values that determine the transformation applied to each pixel. By modifying the matrix, you can control the azimuth and strength of the embossing effect.

The matrix is a 3x3 grid where each element corresponds to the current pixel and its surrounding pixels. The central value represents the current pixel being processed. The filter combines these values based on their weights in the matrix to create the transformed pixel. You can customize the filter by adjusting the scale and offset parameters, which influence the overall strength of the effect.

Example

Here is an example that demonstrates how to apply a custom emboss filter to an image, allowing you to control the appearance of the emboss effect by adjusting various parameters.

from PIL import Image, ImageFilter

# Open the input image 
image = Image.open('Images/TP logo.jpg')

# modify the filterargs such as scale, offset and the matrix
ImageFilter.EMBOSS.filterargs=((3, 3), 2, 150, (0, 0, 0, 0, 1, 0, -2, 0, 0))
    
# Apply an emboss effect to the image
embossed_result = image.filter(ImageFilter.EMBOSS)

# Display the input image
image.show()

# Display the modified image with the emboss effect
embossed_result.show()

Inputput Image

tp logo

Output Image

Output filtered image with custom emboss effect −

emboss effect
Advertisements