ImageOps Module in Pillow

Hello Programers! Continuing our topic on the pillow Python library we take a look at our next module that is the ImageOps module of Python.

This module contains a number of ready-made image processing operations. Also, most of the operators work only on L and RGB images. Different functions in the ImageOps module are given below.

Functions in the ImageOps module

  1. autocontrast(): The autocontrast function is used to normalize the image contrast. It takes 3 parameters that are image, cutoff, and ignore. The image parameter is to process the image. The cutoff value is used to normalize the image contrast. And the ignore parameter is used to ignore the background pixels. This function returns an image.
    from PIL import ImageOps, Image
    
    img = Image.open('pic1.jpg')
    
    img2 = ImageOps.autocontrast(img, cutoff=40)
    img2.show()
    
  2. colorize(): This function is used to colorize the grayscale images. It takes a total of 7 parameters. The 1st parameter is the image which is to be colorized. The 2nd parameter is black which takes black input pixels. Similarly, the 3rd parameter is white which takes white input pixels. The 4th parameter is mid which is used for midtone input pixels. The last 3 parameters are blackpointwhitepoint, and midpoint. These 3 parameters integer input of [0, 255] for there respective mapping. The colorize function returns an image.
    from PIL import ImageOps, Image
    
    img = Image.open('pic1.jpg').convert("L")
    
    img2 = ImageOps.colorize(img, black="red", white="white", mid="yellow")
    img2.show()
    
  3.  scale(): This function is used to rescale a particular image that is given in the parameter. The scale parameter takes 3 inputs. The 1st parameter is an image that is to be rescaled. The 2nd parameter factor which specifies the expansion factor. The factor value between 0 and 1 contracts the image and value greater than 1 expands the image. And the last parameter is resample that specifies which resampling method to use. This function returns an image object.
    from PIL import ImageOps, Image
    
    img = Image.open('pic1.jpg').convert("L")
    
    img2 = ImageOps.scale(img, 15, resample=5)
    img2.show()
    
  4. expand(): This function is used to add a border to an image. It takes 3 parameters. The first parameter is the image to which the border is to be added. The  2nd parameter is the border which takes the width of the border in pixel. And the last parameter is fill which is to add color to the border. It returns an image.
    from PIL import ImageOps, Image
    
    img = Image.open('pic1.jpg').convert("L")
    
    img2 = ImageOps.expand(img, border=5, fill=10)
    img2.show()
  5. grayscale(): This function is used to convert the image to grayscale. It takes a single image input and returns an image.
    from PIL import ImageOps, Image
    
    img = Image.open('pic1.jpg').convert("L")
    
    img2 = ImageOps.grayscale(img)
    img2.show()
  6.  invert(): This function is used to invert the image i.e. to negate the image. Even the invert function takes a single image input and returns an inverted image.
    from PIL import ImageOps, Image
    
    img = Image.open('pic1.jpg').convert("L")
    
    img2 = ImageOps.invert(img)
    img2.show()
  7.  mirror(): This function is used to flip the image horizontally i.e. from left to right. It takes a single image input and returns a mirrored image.
    from PIL import ImageOps, Image
    
    img = Image.open('pic1.jpg').convert("L")
    
    img2 = ImageOps.mirror(img)
    img2.show()
  8. posterize(): This function is used to reduce the number of bits of each color channel. It takes 2 parameters. The 1st parameter being the image and 2nd parameter is bits which keeps the number of bits each channel.
    from PIL import ImageOps, Image
    
    img = Image.open('pic1.jpg').convert("L")
    
    img2 = ImageOps.posterize(img, 3)
    img2.show()
  9. solarize(): This function is used to invert all the pixel value which are above the threshold. It takes 2 parameters of which the 1st parameter is the image. The 2nd parameter is the threshold value.
    from PIL import ImageOps, Image
    
    img = Image.open('pic1.jpg').convert("L")
    
    img2 = ImageOps.solarize(img, threshold=128)
    img2.show()
  10. flip(): This function is used to flip the image vertically i.e. from top to bottom. It takes only one parameter which an image and returns the flipped image.
    from PIL import ImageOps, Image
    
    img = Image.open('pic1.jpg').convert("L")
    
    img2 = ImageOps.flip(img)
    img2.show()
    

Thank You guys for staying till the end. That’s it for this tutorial. We will meet in the next tutorial where I will be covering the ImagePalette module from the pillow library

Leave a Reply

Your email address will not be published.