Create an image using numpy array – random pixel values

As we all know pixel is the smallest unit in a picture and each picture will be made up of pixels. These pixels usually contains values which represents a colour. For example 0 is black and 255 is black. Any value between this will be some shade of gray.

I wanted to create an image using python where each pixel will have a random colour. I used numpy to create the pixel data and pillow library to convert the numpy array to an image.

Installation

First install numpy and the pillow packages using pip.

pip install numpy
pip install pillow

Once we have installed this we are all set.

Code

This is the code snippet to generate the image.

This will generate an image like this.

zooming in on this i will get a piece of art like this.

The “randint()” function generated random values for each pixels and hence we have an image like this with different shades of colours on each pixel.