Conversion experiment 1

From XPUB & Lens-Based wiki
Revision as of 19:32, 12 January 2016 by SN (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Image to text conversion experiment.

The Script converts RGB values of each pixel of the image to its binary representation and maps each digit combination with the letter. All non-alphabetic signs, numbers and white spaces are deleted from the final output.

import os, sys
import binascii
from PIL import Image
import string
letters = set(string.letters)

im = Image.open("molevich.png").convert("RGB")
print(im.format, im.size, im.mode)
pix=im.load()
w=im.size[0]
h=im.size[1]

pict = []
pict_binary = []
for i in range(w):
	for j in range(h):
		a = pix[i,j]
		red = a[0]
		green = a[1]
		blue = a[2]
		pict.append(red)
		pict.append(green)
		pict.append(blue)

for p in pict:
	pict_binary.append('{0:08b}'.format(p))
data_string = ''.join(pict_binary)
n = int(data_string, 2)
m = binascii.unhexlify('%x' % n)
free_text = ''.join(x for x in m if x in letters)
print free_text