Convert a NumPy array to an image


The array created using the Numpy library can be converted into an image using the PIL or opencv libraries in python programming language. Let’s see about each library one by one.

Python Image Library

PIL is abbreviated as Python Image Library, which is an image processing libraries in python. It is a light weight and easy to use library to perform the image processing tasks like reading, writing, resizing and cropping the images.

This library performs all the basic image processing tasks but don’t have any advanced features required for computer vision applications. We have a function in PIL named fromarray() which is used to convert the array into an image.

Syntax

Following is the syntax for using the PIL library fromarray() function to convert the array into an image.

from PIL import Image
Image.fromarray(array) 

Where,

  • PIL is the library.

  • Image is the module.

  • fromarray is the function used to convert the array into image.

  • array is the input array.

Example

In the following example, we are passing the array as the input argument to the Image() function of the PIL library then the array will be converted into an image.

import numpy as np
from PIL import Image
img_array = np.random.randint(0, 256, size=(400, 400, 3), dtype=np.uint8)
img = Image.fromarray(img_array)
img.show()

Output

When we run the above code, following output will be created -

Example

Let’s see another example to work with the Image() function to convert the array into an image.

import numpy as np
from PIL import Image
arr = np.random.random_sample((54,20))-300
print("The created array:",arr)
arr_image = Image.fromarray(arr)
print(arr_image)
arr_image.show()

Output

Following is the output of the Image() function of the PIL library.

The created array: [[-299.5919437  -299.74420221 -299.49075902 ... -299.89184373
  -299.69001867 -299.16309632]
 [-299.19938896 -299.28820797 -299.61738678 ... -299.92440345
  -299.13888282 -299.76989823]
 [-299.00815558 -299.20241227 -299.38977629 ... -299.24134658
  -299.98742918 -299.52568095]
 ...
 [-299.56342592 -299.28958897 -299.49736771 ... -299.52379255
  -299.96158965 -299.87328193]
 [-299.66344304 -299.06209353 -299.12469693 ... -299.77211586
  -299.29320983 -299.11549178]
 [-299.20544152 -299.3039006  -299.44856478 ... -299.37400605
  -299.51143367 -299.14221048]]
<PIL.Image.Image image mode=F size=20x54 at 0x7F3BF0837220>
Error: no "view" mailcap rules found for type "image/png"
/usr/bin/xdg-open: 882: www-browser: Permission denied
/usr/bin/xdg-open: 882: links2: Permission denied
/usr/bin/xdg-open: 882: elinks: Permission denied
/usr/bin/xdg-open: 882: links: Permission denied
/usr/bin/xdg-open: 882: lynx: Permission denied
/usr/bin/xdg-open: 882: w3m: Permission denied
xdg-open: no method available for opening '/tmp/tmprade9ylv.PNG'

Open Source Computer Vision library

Opencv is abbreviated as the Open Source Computer Vision library, which is more advanced library developed to work with the computer vision applications. It has the features such as; object detection, tracking, facial recognition and etc. OpenCV is suitable to perform image and video analysis, augmented reality, robotics, and other computer vision applications.

In opencv, we have a function named imshow() which is used to convert the array into an image.

Syntax

Following is the syntax for converting the array into an image using the imshow().

import cv2
cv2.imshow(image_name,array)

Where,

  • cv2 is the name of the library.

  • imshow is the function used to convert the array into an image.

  • array is the input array.

  • image_name is the name of the converted image.

Example

In order to convert the array into an image, we have to pass the array to be converted and the file name as the input arguments to the imshow() function of the opencv library.

import cv2
import numpy as np
img_array = np.random.randint(0, 256, size=(400, 400, 3), dtype=np.uint8)
cv2.imshow('image', img_array)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Following is the output of the imshow() function of the opencv library, when we run the above code -

Updated on: 09-Aug-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements