Image Processing

NumPy Tutorial: Simple Image Processing Use-Case

In this tutorial, we will walk through how to use NumPy, a powerful library for numerical operations in Python, for basic image processing. NumPy is highly optimized and allows us to perform array operations efficiently. We’ll use the PIL library to read and save images, and NumPy to perform operations on the image data.

Table of Contents

  1. Setup
  2. Loading an Image
  3. Converting to Grayscale
  4. Image Negation
  5. Image Brightness Adjustment
  6. Putting It All Together

1. Setup

First, you’ll need to install NumPy and PIL. Open your terminal and type:

Python
pip install numpy pillow

2. Loading an Image

To read an image using PIL:

Python
from PIL import Image
import numpy as np

# Load the image
image_path = "example.jpg"  # Replace with your image path
image = Image.open(image_path)

3. Converting to Grayscale

Images are often easier to analyze when converted to grayscale. Let’s use NumPy to do that.

Python
def to_grayscale(image):
    image_array = np.array(image)
    gray_array = image_array.mean(axis=2, dtype=np.uint8)
    return Image.fromarray(gray_array, "L")

To convert our image to grayscale:

Python
gray_image = to_grayscale(image)
gray_image.show()  # To display the image

4. Image Negation

Negating an image involves inverting all its pixel values. In a grayscale image, this would mean replacing each pixel (x) with (255 – x).

Python
def negate_image(image):
    image_array = np.array(image)
    negated_array = 255 - image_array
    return Image.fromarray(negated_array, "L")

To negate our grayscale image:

Python
negated_image = negate_image(gray_image)
negated_image.show()

5. Image Brightness Adjustment

You can adjust the brightness by adding a constant to all the pixel values.

Python
def adjust_brightness(image, value):
    image_array = np.array(image)
    image_array = np.clip(image_array + value, 0, 255).astype(np.uint8)
    return Image.fromarray(image_array, "L")

To increase the brightness by 50:

Python
bright_image = adjust_brightness(gray_image, 50)
bright_image.show()

6. Putting It All Together

Now, let’s combine these operations into a main function:

Python
from PIL import Image
import numpy as np

# Helper functions (to_grayscale, negate_image, adjust_brightness) go here

def main():
    image_path = "example.jpg"
    image = Image.open(image_path)

    gray_image = to_grayscale(image)
    gray_image.show()

    negated_image = negate_image(gray_image)
    negated_image.show()

    bright_image = adjust_brightness(gray_image, 50)
    bright_image.show()

if __name__ == "__main__":
    main()

Run the main() function, and you’ll see the original grayscale image, the negated image, and the brightness-adjusted image displayed one after the other.

Congratulations, you’ve successfully used NumPy for simple image processing tasks! This is just the tip of the iceberg. NumPy is incredibly powerful and can be used for more complex image processing tasks, mathematical modeling, machine learning, and much more. Feel free to extend this tutorial and try out more advanced operations.