Images and Arrays in Numpy

The computer cannot understand the colors, and it treats a human photo the same as the photo of the Pluto. A computer understands the image pixel just as a matrix having three-dimension; these dimensions represent the RGB color.

OpenCV reads images as BGR clor rather than RGB


Look at the above image carefully; you can see the square boxes; these are called pixels. Images are nothing but a bunch of pixels; every pixel will have BGR color details. Based on the BGR values, the color of the pixel is decided.

More straightforwardly, in recent times, you might have heard about phones with 48Mega Pixel, 108 Mega Pixel. They manufacturer mean that the photo taken by that camera of the mobile can have up to 48 Millian pixel in it.

The B, G, R colors could range from 0-255 (each) in every pixel

The image in the form of pixel and color.


B, G, R = 0, 0, 0, means black. B, G, R = 255, 255, 255 means White

Relationship between Numpy and Images and Arrays

Now let's see how we handle the images using the numpy. Some people may use the IDE for writing the code, but others might be using Jupiter Notebook / Lab. For this tutorial purpose, I am using the Jupyter lab.

We need to import the libraries/modules to perform images based operations. matplotlib will be useful when we are showcasing the image on the ipynb. Image class from PIL (Pillow) is useful for handle image details.

print() function will not help in printing the images

The below code is used with Spyder IDE; you can use any IDE of your choice.

import numpy as np
import matplotlib as plt
from PIL import Image

pic ="logo.jpg") show the image in default image App of your computer.


imshow() method from the pyplot like below and the image will be shown in IDE.

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
pic ="C:\Users\pnagaraj\Downloads\opencv\logo.png")


The above code works well, but as we are using the Jupyter notebook, it has the capability to show the image on the Notebook itself.

The output from the Jupyter notebook.


I hope you know that in Jupyter notebook can print the last output to the screen. Sometimes I might be using that just like below. So please do use the Jupyter notebook for the remaining of the articles.


Images as Arrays :

Images are nothing but arrays; the height of that image is rows and width is columns. asarray() function from numpy helps to convert the image into an array.

pic ="logo.png")
image_array = np.asarray(pic)


The shape of the pixel arrays:

We can get the shape of the pixel array using the shape property of the array/matrix.

image_array ="C:/Users/pnagaraj/Pictures/opencv/logo.jpg")
pixel_array = np.asarray(image_array)
(663, 1599, 3)

In the above output, 663- is the height of the image, 1599 is the width of the images, 3- Number of color present, nothing but RGB.

Extracting images as solo colors:

We can extract the R, G, B colors from the RGB images. To understand the extraction, you need to know the slicing operations on the array, which we discussed in the Numpy array article.

We can extract every value in a row/column using the : (colon) symbol.

image_array ="C:/Users/pnagaraj/Pictures/opencv/logo.jpg")
pixel_array = np.asarray(image_array)
plt.imshow(pixel_array[:, :, 0])

The absence of a color will be shown as dark, present of the color becomes yellow. This is the reason when there is a red color, the color becomes almost yellow (highlighted with array on images 1, similarly it applies for other two images in the below image).

Below Images show the Extraction of a single color.


Got your question : the absence of color is dark, but how come present of color is yellow.
This not based on black-white images; these colors are based on matplotlib color range for color blind people.

The below image shows the range of color for color blind people. Matplotlib takes the Viridis color for the images that you can in the above image. Reference:


Making a specific color to 0(zero):

Now, let's understand how an image works when color becomes. The extraction of a particular color is different from the absence of other colors.

image_array ="C:/Users/pnagaraj/Pictures/opencv/logo.jpg")
pixel_array = np.asarray(image_array)
copy_pixel_array = pixel_array.copy()
copy_pixel_array[:, :, 1] = 0


In the below images, I made red as 0, in image 1. Green is 0 in image 2. Blue is 0 in image 3.


The below image shows the images where R=0 in image 1. R, G=0 in image 2. R, G, B =0 in image 3.


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Subscribe to See Videos

Subscribe to my Youtube channel for new videos : Subscribe Now