Python Image Processing Overview

By: Stephen Patrick | 20 Oct 2016 | Category: Python Image Processing Introduction

Python Image Processing Overview

Python provides a number of image processing packages. It is also relatively easy to integrate with other image processing libraries. But before getting to that let us quickly review what exactly an image is.

What Is An Image

Every bitmap image consists of a number of pixels. Pixels are the raw data that produce an image. A pixel is the lowest level of granularity. We can consider an image as a grid, where each column contains a pixel. A pixel in each column of the grid holds a value. This value can be considered as the intensity of light or color that appears at a particular location in an image.

Image Color Space Representation

The structure of the grid is dependent on the color space. Let us consider a gray scale image. A gray scale image is an image which contains different shades of gray. A pixel value in a gray scale image has a value in the range from 0 to 255. Where 0 is the black and 255 is white. So if we consider a
a gray scale image of size 20 pixels of width by 10 pixels of height. We have a grid of 20 rows and 10 columns and overall the image will contain 200 pixels, and each pixel will contain a value from 0 to 255. Consider the below image of Saint Padre Pio that was converted from a color image to gray scale. To see the color distribution of pixel values in the range of 0 - 255 of Saint Padre Pio we can use a histogram. We can consider a gray scale image as a two dimensional matrix. Padre Pio Grayscale Python Image Processing Histogram Gray Histogram color In contrast to a gray scale image most images are represented in a three dimensional space. In particular most images are represented in the RGB (Red, Green, Blue) color space. In this space a pixel consists of a red part, a green part and a blue part. Like a gray scale image each part consists of a value in the range of 0 to 255. Likewise, consider the Padre Pio image but this time in color. The histogram is flattened to make it easier to view. When an image is displayed each part of red, green and blue is combined to give an image its color. For example, if a pixel in an image contains a value of (255, 255,255) for red, green, and blue then this pixel will have the color white. Likewise, if a pixel has a value of (0, 0,0) for red, green and blue then the pixel will have the color black.

Representing Images In Python

As we said a pixel holds a value in the range from 0 to 255 in gray scale and RGB color spaces. We can represent this value as an unsigned 8 bit integer. For an RGB image we can represent each pixel as a tuple of (Red, Green, Blue). To represent the complete image we can use an array data structure. In particular, we need a two dimensional data structure. For example for an image that is 10 X 10 in size we would define a two dimensional array with 10 rows and 10 columns. For a gray scale image each cell with have a single value in the range of 0 – 255. Moreover, for an RGB image each cell would contain the tuple (Red, Green, Blue). The first pixel located at the top left of the image would be at (0,0) and the last pixel would be located at (9,9). Note we are using array indexes to index the image. Array indexes in Python are in the range of 0 to Array Length - 1.