15

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?

7
  • Possible duplicate Commented Aug 19, 2015 at 21:30
  • 2
    possible duplicate of how can I use the python imaging library to create a bitmap Commented Aug 19, 2015 at 21:42
  • 1
    @ap - The duplicate question was limiting the scope of the answer to the Python Imaging Library, when there are other ways to answer the question. Commented Aug 19, 2015 at 21:51
  • 1
    @Back2Basics asker specifically requests PIL responses, and has tagged his question with python-imaging-library Commented Aug 19, 2015 at 21:55
  • 2
    ...and yet the asker picked the matplotlib answer. "...People don't know what they want till you show it to them." - Steve Jobs Commented Aug 19, 2015 at 22:01

4 Answers 4

21

You can use Image.new with 1 mode and put each integer as pixel in your initial image:

>>> from PIL import Image
>>> import random

>>> data = [random.choice((0, 1)) for _ in range(2500)]
>>> data[:] = [data[i:i + 50] for i in range(0, 2500, 50)]
>>> print data
[[0, 1, 0, 0, 1, ...], [0, 1, 1, 0, 1, ...], [1, 1, 0, 1, ...], ...]

>>> img = Image.new('1', (50, 50))
>>> pixels = img.load()

>>> for i in range(img.size[0]):
...    for j in range(img.size[1]):
...        pixels[i, j] = data[i][j]

>>> img.show()
>>> img.save('/tmp/image.bmp')

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Upvoting since this is the only answer not to use external libraries not mentioned in the original question or tags.
I get SystemError: new style getargs format but argument is not a tuple on this line: pixels[i, j] = data[i][j]. it seems the assigned value must be a tuple so this fixes: pixels[i, j] = data[i][j],
12

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

2 Comments

This works well for me! Only problem is the output is red and blue, not black and white. Any thoughts?
By default it saves array in jet colormap, black and white would be gray colormap. Will update the answer when i get back to my machine
4

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)

Comments

3
import scipy.misc
import numpy as np
data = [1,0,1,0,1,0...]
data = np.array(data).reshape(50,50)
scipy.misc.imsave('outfile.bmp', data)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.