Criando imagem a partir de vetor de bits

No meu trabalho de Reconhecimento de padrões, eu precisei utilizar uma base que me fornecia imagens como vetor de bits. Por exemplo, o f  era representado pelo seguinte vetor:

0000000000000000000001111110000
0000000000000000000001111111100
0000000000000000000001111111110
0000000000000000000001111111111
0000000000000000000111111111110
0000000000000000000111111111110
0000000000000000000111111111111
0000000000000000000011111111111
0000000000000000000111111111111
0000000000000000001111111111110
0000000000000000000111111111111
0000000000000000000111111111111
0000000000000000000111111111111
0000000000000000001111111111111
0000000000000000001111111111111
0000000000000000011111111111110
0000000000000000011111111111110
0000000000000000011111111111100
0000000000000000011111111111100
0000000000000000001111111111100
0000000000000000011111111111000
0000000000000000011111111111000
0000000000000000011111111111000
0000000000000000111111111111000
0000000000000000111111111110000
0000000000000111111111111100000
0000000000011111111111111000000
0000000000011111111111111000000
0000000000111111111111111000000
0000000011111111111111111000000
0000001111111111111111110000000
0000011111111111111111110000000
0000111111111111111111110000000
0011111111111111111111110000000
1111111111111111111111110000000
1111111111111111111111111000000
1111111111111111111111111000111
1111111111111111111111111111111
1111111111111111111111111111111
1111111111111111111111111111111
1111110000000111111111111111111
1111100000000111111111111111111
1100000000000011111111111111111
0000000000000001111111111111111
0000000000000001111111111111110
0000000000000000111111111111000
0000000000000001111111100000000
0000000000000000111111110000000
0000000000000000111111110000000
0000000000000000111111110000000
0000000000000000111111111000000
0000000000000000111111111000000
0000000000000000111111111000000
0000000000000000111111111100000
0000000000000000111111111110000
0000000000000000111111111110000
0000000000000000111111111111000
0000000000000001111111111110000
0000000000000000111111111111000
0000000000000000111111111111000
0000000000000000111111111111100
0000000000000000011111111111100
0000000000000000111111111111000
0000000000000000011111111111100
0000000000000000011111111111100
0000000000000000001111111111100
0000000000000000001111111111100
0000000000000000001111111111100
0000000000000000001111111111100
0000000000000000001111111111100
0000000000000000000111111111100
0000000000000000000011111111100
0000000000000000000011111111100
0000000000000000000001111111100
0000000000000000000000111111100
0000000000000000000000001111100

Infelizmente, eu estava utilizando uma framework que necessitava receber a imagem e não o vetor de bits que é extraído dela. Logo, eu tinha que dá um passo pra trás pra puder dá dois pra frente.

Fazendo uma pesquisa na internet, encontrei uma função do scipy que fazia exatamente o que eu queria.  scipy.misc.imsave cria uma imagem a partir de um array da classe numpy.

A partir disso, eu criei o seguinte código:

#!/bin/bash

import scipy
import numpy

def create_bynary_image(binary_content, output_name):
    letter = open(bynary_content)
    bits_image = []

    for line in letter:
        bits_image.append(map(lambda x: int(x), list(line.replace('n',''))))

    np_array = numpy.array(bits_image)
    letter.close()
    scipy.misc.imsave(output_name, np_array)

if __name__ == '__main__':
    create_bynary_image('letter.txt', 'f.png')
   

Abaixo o conteúdo de letter.txt que é usado no exemplo.

0000000000000000000001111110000
0000000000000000000001111111100
0000000000000000000001111111110
0000000000000000000001111111111
0000000000000000000111111111110
0000000000000000000111111111110
0000000000000000000111111111111
0000000000000000000011111111111
0000000000000000000111111111111
0000000000000000001111111111110
0000000000000000000111111111111
0000000000000000000111111111111
0000000000000000000111111111111
0000000000000000001111111111111
0000000000000000001111111111111
0000000000000000011111111111110
0000000000000000011111111111110
0000000000000000011111111111100
0000000000000000011111111111100
0000000000000000001111111111100
0000000000000000011111111111000
0000000000000000011111111111000
0000000000000000011111111111000
0000000000000000111111111111000
0000000000000000111111111110000
0000000000000111111111111100000
0000000000011111111111111000000
0000000000011111111111111000000
0000000000111111111111111000000
0000000011111111111111111000000
0000001111111111111111110000000
0000011111111111111111110000000
0000111111111111111111110000000
0011111111111111111111110000000
1111111111111111111111110000000
1111111111111111111111111000000
1111111111111111111111111000111
1111111111111111111111111111111
1111111111111111111111111111111
1111111111111111111111111111111
1111110000000111111111111111111
1111100000000111111111111111111
1100000000000011111111111111111
0000000000000001111111111111111
0000000000000001111111111111110
0000000000000000111111111111000
0000000000000001111111100000000
0000000000000000111111110000000
0000000000000000111111110000000
0000000000000000111111110000000
0000000000000000111111111000000
0000000000000000111111111000000
0000000000000000111111111000000
0000000000000000111111111100000
0000000000000000111111111110000
0000000000000000111111111110000
0000000000000000111111111111000
0000000000000001111111111110000
0000000000000000111111111111000
0000000000000000111111111111000
0000000000000000111111111111100
0000000000000000011111111111100
0000000000000000111111111111000
0000000000000000011111111111100
0000000000000000011111111111100
0000000000000000001111111111100
0000000000000000001111111111100
0000000000000000001111111111100
0000000000000000001111111111100
0000000000000000001111111111100
0000000000000000000111111111100
0000000000000000000011111111100
0000000000000000000011111111100
0000000000000000000001111111100
0000000000000000000000111111100
0000000000000000000000001111100
e o resultado foi

teste

RL

 


Warning: Missing argument 1 for cwppos_show_review(), called in /home/rodrigolira/blog.rodrigolira.net/wp-content/themes/flat/content-single.php on line 29 and defined in /home/rodrigolira/blog.rodrigolira.net/wp-content/plugins/wp-product-review/includes/legacy.php on line 18