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

How can I generate a Binary image with the about matrics, in PBM formate? As there are 16 columns and 5 rows. I'hv searched a few methods that can convert an image into binary one but not able to find a way to generate a binary image using this kinda matrix.

and

How much amount of memory will be used to represent a 0 or 1 on a memory in a binary image? Let's suppose a binary image only has 1 pixel. How much memory will be used to represent that one pixel on the image? As in the case of the grayscale image, It would take 8 bytes for a single pixel. In the case of the binary image, where it only takes either 0 or 1, doe it takes 2 bits two represent a single pixel?

Thanks.

Miki
  • 40,887
  • 13
  • 123
  • 202
jax
  • 3,927
  • 7
  • 41
  • 70

1 Answers1

2

You could use the Pillow library in order to create an pbm image from the above data:

from PIL import Image

binaryData = [[0,1,0,1,1,0,0,0,0,1,0,1,0,1,0,1],
[0,0,0,0,0,1,1,1,1,0,1,0,1,0,1,1],
[0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,0],
[0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0],
[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]]

pixelData = [255 if elem else 0 for row in binaryData for elem in row]

img = Image.new("L", (len(binaryData[0]), len(binaryData)))
img.putdata(pixelData)
img.save("image.pbm")
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
  • How much amount of memory will be used to represent a 0 or 1 on a memory? As in the case of the grayscale image, It would take 8 bytes for a single pixel. In the case of the binary image, where it only takes either 0 or 1, doe it takes 2 bits two represent a single pixel? – jax Dec 07 '18 at 22:06
  • I have a small update in Question can you answer that part? – jax Dec 07 '18 at 22:09
  • @jax in the implementation above, each pixel has all three R,G,B values so it will take 24 bits for each pixel (8 bits per value). – Vasilis G. Dec 07 '18 at 22:10
  • is it possible to plot it only with binary value like 0 black and 1 white without using RGB. withing only 2bit Max. – jax Dec 07 '18 at 22:16
  • You could convert it in grayscale by writing `img.convert('LA')` right before saving. You can check Pillow's [repository](https://github.com/python-pillow/Pillow/blob/master/src/PIL/Image.py#L271) to see the available modes. – Vasilis G. Dec 07 '18 at 22:26
  • 1
    Why `(0,0,0)`, `(255,255,255)` and `Image.new('RGB')` for a greyscale image? Why not `0`, `255` and `Image.new('L')`? – Mark Setchell Dec 08 '18 at 12:51
  • 1
    @MarkSetchell you could do that as well. Edited my question. – Vasilis G. Dec 08 '18 at 13:39