Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Flip and Paste an Image Using the Pillow Library

Flip and Paste an Image Using Pillow

Condition for How to Flip and Paste an Image Using the Pillow Library

  • Description: This guide demonstrates how to flip and paste images using the Pillow library in Python. Pillow is an open-source Python Imaging Library (PIL) fork that adds image processing capabilities to your Python interpreter. In this tutorial, you will learn how to flip an image horizontally or vertically and then paste one image onto another using Pillow.
Why Should We Choose Pillow for Image Processing?
  • User-friendly: Pillow provides simple, intuitive functions for basic image manipulation tasks.
  • Cross-platform: Works on multiple operating systems such as Windows, Linux, and macOS.
  • Comprehensive: Offers extensive image manipulation features, including resizing, cropping, filtering, and drawing.
  • Active Development: Pillow is actively maintained and improved, with a large community contributing to its development.
Step by Step Process
  • Install Pillow Library: First, you need to install the Pillow library using pip.
  • Open Images: Load the images you want to work with.
  • Flip Image: Flip the image horizontally or vertically.
  • Paste Image: Paste the flipped image onto another image at a specified location.
  • Save or Display Image: Save the modified image to your disk or display it using the show() method.
Sample Source Code
  • # Import Libraries
    from PIL import Image, ImageEnhance, ImageFilter
    import os

    # Define the folder path where the images are stored
    image_folder = '/home/soft15/soft15/Python/py_Exercises/python_Machine_Learning/25-11-2024/33.flip and paste an image using pillow/img'

    # Get a list of all image files in the folder
    image_files = [f for f in os.listdir(image_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]

    # Loop over each image in the folder
    for image_name in image_files:
        # Full path of the image
        image_path = os.path.join(image_folder, image_name)

        # Open an image file
        with Image.open(image_path) as img:
            # Show the original image
            img.show(title=f"Original Image: {image_name}")

            # 1. Enhance Brightness (for example)
            enhancer = ImageEnhance.Brightness(img)
            bright_img = enhancer.enhance(1.5) # Adjust the factor for brightness enhancement
            bright_img.show(title=f"Brightened Image: {image_name}")

            # 2. Flip the image horizontally
            flipped_img = bright_img.transpose(Image.FLIP_LEFT_RIGHT)
            flipped_img.show(title=f"Flipped Image: {image_name}")

            # 3. Save the processed image
            output_dir = os.path.join(image_folder, 'processed')
            os.makedirs(output_dir, exist_ok=True)

            # Save the flipped image (you can save the brightened or original if preferred)
            flipped_img.save(os.path.join(output_dir, f"flipped_{image_name}"))

    print("Image processing completed!")
Screenshots
  • Flip and Paste an Image Using Pillow