Question: I have an array of binary numbers in Python:

```
data = [0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1...]

```

I would like to take this data out and save it as a bitmap, with a '0' corresponding to white and a '1' corresponding to black. I know that there are 2500 numbers in the array, corresponding to a 50x50 bitmap. I've downloaded and installed PIL, but I'm not sure how to use it for this purpose. How can I convert this array into the corresponding image?

Answer: 
The `numpy` and `matplotlib` way of doing it would be:

```
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
plt.imsave('filename.png', np.array(data).reshape(50,50), cmap=cm.gray)

```

See [this](http://matplotlib.org/api/image_api.html#matplotlib.image.imsave)
**Not for binary array**

In case you have **binary data** in this format - `b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00...`, then you may use this method to write it as an image file:

```
with open("image_name.png", "wb") as img:
    img.write(binary_data)

```