Image Processing with Gaussian Blur, Thresholding, and Inversion

  • Share this:

Code introduction


This function takes an input image path, applies Gaussian blur, threshold filtering, and color inversion, and then saves the processed image to an output image path.


Technology Stack : Pillow library

Code Type : Image processing

Code Difficulty : Intermediate


                
                    
from PIL import Image, ImageFilter, ImageOps

def apply_filters_to_image(input_image_path, output_image_path):
    # Open the input image
    with Image.open(input_image_path) as img:
        # Apply a Gaussian blur filter
        blurred_img = img.filter(ImageFilter.GaussianBlur(radius=5))
        
        # Apply a color threshold filter
        thresholded_img = blurred_img.point(lambda p: 255 if p > 128 else 0, '1')
        
        # Invert the colors of the image
        inverted_img = ImageOps.invert(thresholded_img)
        
        # Save the processed image
        inverted_img.save(output_image_path)                
              
Tags: