Create an Image Using Numpy Array in Python Pillow

In this tutorial, we will learn how to create an image from numpy array using python pillow.

1. Import library

from PIL import Image
import numpy as np

2. Create a numpy array to contain image data

w,h=512,512
t=(h,w,3)
A=np.zeros(t,dtype=np.uint8)

3. Set image data for each pixel

for i in range(h):
    for j in range(w):
        A[i,j]=[i%64,j%128,(i+j)%256]

4. Create an image using numpy array

i=Image.fromarray(A,"RGB")
i.show()

You will find:

Convert a Numpy Array to Image Using Python Pillow