Convert list of lists of 1 and 0 in black and white image in Python

Viewed 204

I would like to know if there is a simple way to convert a simple list of 0 and 1 for example:

[[1, 1, 0, 0, 0, 0, 1, 1],
 [1, 0, 1, 1, 1, 1, 0, 1],
 [0, 1, 0, 1, 1, 0, 1, 0],
 [0, 1, 1, 1, 1, 1, 1, 0],
 [0, 1, 0, 1, 1, 0, 1, 0],
 [0, 1, 1, 0, 0, 1, 1, 0],
 [1, 0, 1, 1, 1, 1, 0, 1],
 [1, 1, 0, 0, 0, 0, 1, 1]]

Into a black and white image, for example for the previous list, this image : smiley

Thanks for your help!

1 Answers

You can simply use matplolib (I named as X your input matrix):

import matplotlib.pyplot as plt
im = plt.imshow(X, cmap='Greys')
plt.show()

enter image description here

Related