Image Filter and Flip Application

  • Share this:

Code introduction


This function accepts an image path, a filter type, and a flip type. It opens the image, applies the selected filter, then flips the image based on the selected flip type, and finally saves the modified image.


Technology Stack : Pillow library

Code Type : Image processing

Code Difficulty : Intermediate


                
                    
from PIL import Image, ImageFilter, ImageOps

def apply_filter_and_flip(image_path, filter_type, flip_type):
    # Open the image
    with Image.open(image_path) as img:
        # Apply the selected filter
        if filter_type == 'BLUR':
            filtered_img = img.filter(ImageFilter.BLUR)
        elif filter_type == 'CONTOUR':
            filtered_img = img.filter(ImageFilter.CONTOUR)
        else:
            raise ValueError("Unsupported filter type")

        # Flip the image based on the selected flip type
        if flip_type == 'HORIZONTAL':
            flipped_img = ImageOps.mirror(filtered_img)
        elif flip_type == 'VERTICAL':
            flipped_img = ImageOps.flip(filtered_img)
        else:
            raise ValueError("Unsupported flip type")

        # Save the modified image
        flipped_img.save('modified_image.jpg')                
              
Tags: