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

Office Address

Social List

How to Perform Image Processing Using Pillow in Python?

Image Processing with Pillow

Condition for Image Processing with Pillow

  • Image processing involves analyzing and manipulating images. Pillow, a Python library, simplifies working with image files, including loading, transforming, and saving them. It supports numerous formats, enabling efficient image editing.
Step by Step Process
  • Import Library: Install and import Pillow in Python.
  • Load Image: Use `Image.open()` to read images.
  • Process Image: Perform operations like resizing, rotating, and filtering.
  • Save Image: Save the processed images to disk.
Why Should We Choose Pillow?
  • Ease of Use: Simple APIs make image manipulation straightforward.
  • Wide Operations: Supports cropping, resizing, and applying filters.
  • Format Support: Compatible with various image formats.
Sample Source Code
  • from PIL import Image, ImageEnhance, ImageFilter
    import os

    # Define the folder path where the images are stored
    image_folder = '31.image processing with 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. Convert Image to Grayscale
      grayscale_img = img.convert('L')
      grayscale_img.show(title=f"Grayscale: {image_name}")

      # 2. Resize Image
      resized_img = img.resize((300, 300))
      resized_img.show(title=f"Resized: {image_name}")

      # 3. Rotate Image (90 degrees)
      rotated_img = img.rotate(90)
      rotated_img.show(title=f"Rotated: {image_name}")

      # 4. Apply Gaussian Blur
      blurred_img = img.filter(ImageFilter.GaussianBlur(radius=5))
      blurred_img.show(title=f"Blurred: {image_name}")

      # 5. Enhance Image Brightness
      enhancer = ImageEnhance.Brightness(img)
      bright_img = enhancer.enhance(1.5) # Increase brightness by 1.5x
      bright_img.show(title=f"Brightened: {image_name}")

      # 6. Enhance Image Contrast
      enhancer = ImageEnhance.Contrast(img)
      contrast_img = enhancer.enhance(2.0) # Increase contrast by 2x
      contrast_img.show(title=f"High Contrast: {image_name}")

      # 7. Save Processed Images
      output_dir = os.path.join(image_folder, 'processed')
      os.makedirs(output_dir, exist_ok=True)

      # Save each processed image in the 'processed' folder
      grayscale_img.save(os.path.join(output_dir, f"grayscale_{image_name}"))
      resized_img.save(os.path.join(output_dir, f"resized_{image_name}"))
      rotated_img.save(os.path.join(output_dir, f"rotated_{image_name}"))
      blurred_img.save(os.path.join(output_dir, f"blurred_{image_name}"))
      bright_img.save(os.path.join(output_dir, f"brightened_{image_name}"))
      contrast_img.save(os.path.join(output_dir, f"contrast_{image_name}"))

    print("Image processing completed!")
Screenshots
  • Image Processing Screenshot