 ## How to convert NumPy array into an Image

Images are stored in a computer as arrays or matrices. An image with a resolution of 256×256 has 256 rows and 256 columns. Using python programming, we can easily produce these types of images on the computer. Python is a popular programming language due to the enormous number of libraries available. NumPy is a Python library for creating arrays and performs mathematical operations on them. This blog will show you how to generate NumPy arrays and save them as images.

Images are stored in a computer as arrays or matrices. An image with a resolution of 256×256 has 256 rows and 256 columns. Using python programming, we can easily produce these types of images on the computer. Python is a popular programming language due to the enormous number of libraries available. NumPy is a Python library for creating arrays and performs mathematical operations on them. This blog will show you how to generate NumPy arrays and save them as images.

## Example 1:

The following are the steps to convert a NumPy array to an image:

• Define width and height of the numpy array
• Create numpy array using numpy.zeros()
• Convert numpy arrary into image using Image.fromarray()
• Save the image
• Display the image
``````from PIL import Image
import numpy

height, width = 224, 224
np_array = numpy.zeros((height, width, 3), dtype=np.uint8)

img = Image.fromarray(np_array, 'RGB')

img.save('black_image.png')
img.show()``````

## Example 2:

The following code will generate a black image of 512×512 size. A green box will appear on this image. You can also use different colors such as Red, Green and Blue etc. for creating images.

``````from PIL import Image
import numpy

height, width = 512, 512
np_array = numpy.zeros((height, width, 3), dtype=np.uint8)
np_array[0:256, 0:256] = [0, 255, 0]

img = Image.fromarray(np_array, 'RGB')

img.save('green_box.png')
img.show()``````