Conversion experiment 1

From XPUB & Lens-Based wiki
Revision as of 17:46, 24 November 2015 by SN (talk | contribs) (Created page with "Image to text conversion experiment. <br /> <source lang="python"> import os, sys import binascii from PIL import Image import string letters = set(string.letters) im = Imag...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Image to text conversion experiment.

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

im = Image.open("RotterdamHype.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