Understanding a digital image using Python: An Image Processing Introduction

In this article, I will discuss how could we do basic digital image processing using Python and extract information from images.

It is known that digital images are digital representations of images and are usually represented as n-dimensional matrix values called pixels. Let’s try it in this “futuristic” image of Guinan and Captain Jean-Luc Picard of the USS Enterprise (yes, because I am a Star Trek fan) and extract its pixel values:

Guinan & Captain Jean-Luc Picard aboard USS Enterprise-D
## Import the image using imread
guinanpicard = imread(‘guinanpicard.jpg’)
guinanpicard
## Output of guinanpicard
array([[[ 73, 81, 156],
[ 73, 83, 154],
[ 67, 80, 148],
...,
[ 44, 22, 24],
[ 36, 17, 19],
[ 32, 16, 29]],

...,

[[181, 159, 208],
[182, 157, 215],
[185, 157, 215],
...,
[ 13, 3, 12],
[ 12, 2, 11],
[ 8, 1, 8]]], dtype=uint8)

We can see that the digital image above can be actually represented as a three-dimensional matrix values. But wait, what are those values that we see here? Those three values that we see are the red , green , and blue color channel values of each pixel. We could view the image based on the specific color channel values of the image using the code below:

## Getting each color channel values
fig, ax = plt.subplots(1, 3, figsize=(12,4))
ax[0].imshow(guinanpicard[:,:,0], cmap=’Reds’)
ax[0].set_title(‘Red’)
ax[1].imshow(guinanpicard[:,:,1], cmap=’Greens’)
ax[1].set_title(‘Green’)
ax[2].imshow(guinanpicard[:,:,2], cmap=’Blues’)
ax[2].set_title(‘Blue’);
RGB Color Channels

The results above shows how our image will look like if we separate each of the red , green , and blue color channels on their own. The dark areas means high values while the white areas means low values on that specific color channel. Besides from RGB, we can also extract the hue , saturation , and value or HSV from our image using the code below:

## Extract the HSV from RGB
guinanpicard_hsv = rgb2hsv(guinanpicard)
## Getting each of HSV
fig, ax = plt.subplots(1, 3, figsize=(12,4))
ax[0].imshow(guinanpicard_hsv[:,:,0], cmap='hsv')
ax[0].set_title('Hue')
ax[1].imshow(guinanpicard_hsv[:,:,1], cmap='gray')
ax[1].set_title('Saturation')
ax[2].imshow(guinanpicard_hsv[:,:,2], cmap='gray')
ax[2].set_title('Value');
HSV Channels

As you may have noticed as well, besides from extracting RGB and HSV channels we could also convert the color into a grayscale image using the following code:

## Converting the color image to grayscale
guinanpicard_gray = rgb2gray(guinanpicard)
imshow(guinanpicard_gray);
Grayscale version converted from the original color image

We could further convert the image from grayscale to monochrome or binary image format by applying thresholding to the image. Thresholding is setting the the pixel value to 255 (white) if the pixel value is greater than the threshold value (this value usually varies, but for this case we will use the mean pixel value) while the other pixel values would be set to 0 (black)

# Set threshold to average value
guinanpicard_binary = img_as_uint(guinanpicard_gray > guinanpicard_gray.mean())
imshow(guinanpicard_binary);
Binary format converted from grayscale format

As you can see on the binary image, the face of Picard cannot be seen anymore due to the pixel values within that area is greater than the given threshold value.

And that’s it! By manipulating our digital image we were able to understand (a little bit) as to what makes the image like that.

A developer by day, a programmer by night

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store