Image Filtering and Horizontal Flip

  • Share this:

Code introduction


This function takes an image path and a filter type, applies the specified filter to the image, and then flips the image horizontally.


Technology Stack : Pillow

Code Type : Image processing

Code Difficulty : Intermediate


                
                    
from PIL import Image, ImageFilter, ImageOps

def apply_filter_and_flip(image_path, filter_type):
    """
    This function takes an image path and a filter type, applies the specified filter to the image,
    and then flips the image horizontally.
    """
    with Image.open(image_path) as img:
        if filter_type == "BLUR":
            filtered_img = img.filter(ImageFilter.BLUR)
        elif filter_type == "CONTOUR":
            filtered_img = img.filter(ImageFilter.CONTOUR)
        elif filter_type == "EMBOSS":
            filtered_img = img.filter(ImageFilter.EMBOSS)
        else:
            raise ValueError("Unsupported filter type")

        flipped_img = ImageOps.mirror(filtered_img)
        flipped_img.save("output_flipped_filtered.jpg")                
              
Tags: