StudyLover
  • Home
  • Study Zone
  • Profiles
  • Contact us
  • Sign in
StudyLover Program for Image Processing with Pillow
Download
  1. Python
  2. Pyhton MCA (Machine Learning using Python)
  3. Programs
Program for Interactive Visualization with Bokeh : Program calculates Haralick texture features
Programs

# main.py

# A demonstration of basic image processing using the Pillow library.

#

# Before running, you may need to install Pillow and Matplotlib:

# pip install Pillow matplotlib


import os

from PIL import Image, ImageDraw, ImageFilter

import matplotlib.pyplot as plt


print("--- Starting Image Processing Demonstration with Pillow ---")


# --- Section 1: Create a Sample Image ---

# To make this script self-contained, we'll create a simple image to process.

# In a real-world scenario, you would open an existing image file.

IMAGE_FILENAME = "sample_image.png"

try:

    # Create a new blank image (200x200 pixels) with a white background

    img = Image.new('RGB', (200, 200), 'white')

    draw = ImageDraw.Draw(img)

    

    # Draw a red rectangle on the image

    # The format is [x0, y0, x1, y1]

    draw.rectangle([50, 50, 150, 150], fill='red', outline='black')

    img.save(IMAGE_FILENAME)

    print(f"\n--- 1. Sample image '{IMAGE_FILENAME}' created successfully. ---")

except Exception as e:

    print(f"An error occurred while creating the sample image: {e}")



# --- Section 2: Open and Display the Image ---

try:

    with Image.open(IMAGE_FILENAME) as img:

        print("\n--- 2. Opening and displaying the original image. ---")

        plt.imshow(img)

        plt.title('Original Image')

        plt.show()


        # --- Section 3: Manipulate the Image ---

        print("\n--- 3. Manipulating the image. ---")


        # a) Rotate the image by 45 degrees

        rotated_img = img.rotate(45, expand=True, fillcolor='white')

        print("- Image rotated 45 degrees.")

        plt.imshow(rotated_img)

        plt.title('Rotated Image')

        plt.show()


        # b) Convert the image to grayscale

        grayscale_img = img.convert('L')

        print("- Image converted to grayscale.")

        plt.imshow(grayscale_img, cmap='gray')

        plt.title('Grayscale Image')

        plt.show()


        # c) Apply a filter (Gaussian Blur)

        blurred_img = img.filter(ImageFilter.GaussianBlur(radius=5))

        print("- Gaussian blur filter applied.")

        plt.imshow(blurred_img)

        plt.title('Blurred Image')

        plt.show()


        # --- Section 4: Save the Final Processed Image ---

        PROCESSED_FILENAME = "processed_image.png"

        blurred_img.save(PROCESSED_FILENAME)

        print(f"\n--- 4. Final blurred image saved as '{PROCESSED_FILENAME}'. ---")


except FileNotFoundError:

    print(f"Error: The file '{IMAGE_FILENAME}' could not be found.")

except Exception as e:

    print(f"An error occurred during image processing: {e}")


# --- Clean up the created image files ---

finally:

    print("\n--- Cleaning up created image files. ---")

    for filename in [IMAGE_FILENAME, 'processed_image.png']:

        if os.path.exists(filename):

            os.remove(filename)

            print(f"Removed '{filename}'")


print("\n--- End of Demonstration ---")


Program for Interactive Visualization with Bokeh Program calculates Haralick texture features
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website