Logarithmic Transformation Of An Image Using Python And Pillow


  • The logarithmic transform of a digital image is given by 

 s=T(r) = c*log(r+1)

            's' is the output image

            'r' is the input image        

  • When logarithmic transformation is applied onto a digital image, the darker intensity values are given brighter values thus making the details present in darker or gray areas of the image more visible to human eyes.
  • The logarithmic transformation also scales down the brighter intensity values to lower values.
  • However, the brighter intensity values are not scaled down to the extent the darker intensity values are scaled up.
  • ‘c’ is the scaling constant.
  • For a digital image with intensity values ranging from 0 to 255 the transformation log(r+1) produces value in the range of 0 to 2.41.
  • Representing pixel values in a smaller range of values from 0 to 2.41 is difficult to comprehend.
  • Hence, a scaling operation is required to represent the logarithmically transformed intensity values in a bigger range. The scaling constant ‘c’ does this.
  • The scaling constant is provided by

c = 255/log(1+Maximum pixel value from the input image).



#----- Example Python program for logarithmic transformation of a Digital Image -----


# import Pillow modules

from PIL import Image

from PIL import ImageFilter

import math


# Compute log

def logTransform(c, f):

    g = c * math.log(float(1 + f),10);

    return g;


# Apply logarithmic transformation for an image  

def logTransformImage(image, outputMax = 255, inputMax=255):

    c = outputMax/math.log(inputMax+1,10);


    # Read pixels and apply logarithmic transformation

    for i in range(0, img.size[0]-1):

        for j in range(0, img.size[1]-1):

            # Get pixel value at (x,y) position of the image

            f = img.getpixel((i,j));


            # Do log transformation of the pixel

            redPixel    = round(logTransform(c, f[0]));

            greenPixel  = round(logTransform(c, f[1]));

            bluePixel   = round(logTransform(c, f[2]));


            # Modify the image with the transformed pixel values

            img.putpixel((i,j),(redPixel, greenPixel, bluePixel));


    return image;


# Display the original image

imageFileName = "forest.jpg";

img = Image.open(imageFileName);



# Display the image after applying the logarithmic transformation

logTransformedImage = logTransformImage(img);




Before applying logarithmic transformation on a Digital Image using Python and Pillow:

Before applying Logarithmic transformation to a digital image using Python


After applying logarithmic transformation on a Digital Image using Python and Pillow:

In the original image the trees are darker and a small region of a stream or a path is not visible. Many trees as well are not clearly visibile.

The logarithmically transformed image has the most of the trees distinctly visible. However the background snow corresponding to the brightest pixels(not the foreground snow) is mostly the same in both the images. 

After applying Logarithmic transformation to a digital image using Python

Copyright 2020 © pythontic.com