Including a binary string in an image using PIL

So, I'm trying to make PIL create an image based on a line of binary code. Some background:

from PIL import Image value = "0110100001100101011011000110110001101111" vdiv = [value[i:i+8] for i in range(0, len(value), 8)] 

^ This creates a list of bytes from a binary string. ['01101000', '01100101',.....]

 def diff(inp): if inp == '1': return (0,0,0) if inp == '0': return (255,255,255) else: pass 

^ This returns a color tuple for each corresponding bit, and if I call:

 for i in vdiv: for i2 in i: print diff(i2) 

It will print each color tuple for each bit in the bytes listed. (0,0,0) (0,0,0) (255,255,255)...

I want to know how can I get a PIL to create an image where the pixels correspond to a binary string?

Here's how it should look. : screenshot

What I still did for PIL:

 img = Image.new( 'RGB', (8,len(vdiv)), "white") pixels = img.load() ## for x in range(img.size[0]): for y in range(img.size[1]): for i in vdiv: for i2 in i: pixels[x,y] = diff(i2) #Creates a black image. What do? ## img.show() 

This is the for x in range bit that bothers me. I am lost.

+3
python binary image python-imaging-library
source share
1 answer

You can use img.putdata :

 import Image value = "0110100001100101011011000110110001101111" cmap = {'0': (255,255,255), '1': (0,0,0)} data = [cmap[letter] for letter in value] img = Image.new('RGB', (8, len(value)//8), "white") img.putdata(data) img.show() 

enter image description here


If you have NumPy, you can use Image.fromarray :

 import Image import numpy as np value = "0110100001100101011011000110110001101111" carr = np.array([(255,255,255), (0,0,0)], dtype='uint8') data = carr[np.array(map(int, list(value)))].reshape(-1, 8, 3) img = Image.fromarray(data, 'RGB') img.save('/tmp/out.png', 'PNG') 

but this time test allows you to use putdata faster:

 value = "0110100001100101011011000110110001101111"*10**5 def using_fromarray(): carr = np.array([(255,255,255), (0,0,0)], dtype='uint8') data = carr[np.array(map(int, list(value)))].reshape(-1, 8, 3) img = Image.fromarray(data, 'RGB') return img def using_putdata(): cmap = {'0': (255,255,255), '1': (0,0,0)} data = [cmap[letter] for letter in value] img = Image.new('RGB', (8, len(value)//8), "white") img.putdata(data) return img 

 In [79]: %timeit using_fromarray() 1 loops, best of 3: 1.67 s per loop In [80]: %timeit using_putdata() 1 loops, best of 3: 632 ms per loop 
+1
source share

All Articles