raster (PIL)

superpollo user at example.net
Thu Jul 23 08:41:23 EDT 2009


Diez B. Roggisch wrote:
> superpollo wrote:
...
>>high = len(raster_lines)
>>wide = len(raster_lines[0])
>>bytes_in_a_row = wide/bits_in_a_byte
> 
> 
> This will give you the wrong result if not divideable by bits_in_a_byte. 
> 

then maybe:

#!/usr/bin/env python
import Image
import sys
bits_in_a_byte = 8
raster_string = """\
0000000000000000000000
00100100111100100000100000111100
00100100100000100000100000100100
00111100111000100000100000100100
00100100100000100000100000100100
00100100111100111100111100111100
00000000000000000000000000000000
00000000000000000000000000000000
00000000111100100000111100000000
00000000100000100000100100000000
00000000100000100000111100000000
00000000100000100000100000000000
00000000111100111100100000000000
00000000000000000000000000000000
"""
raster_lines = raster_string.splitlines()
high = len(raster_lines)
wide = len(raster_lines[0])
bytes_in_a_row = wide/bits_in_a_byte
wide_for_real = bytes_in_a_row*bits_in_a_byte
if wide_for_real:
     bitmap = ""
     for raster_line in raster_lines:
         for byte_count in range(bytes_in_a_row):
             first_bit = byte_count*bits_in_a_byte
             bitmap += 
chr(int(raster_line[first_bit:first_bit+bits_in_a_byte] ,
2))
     im = Image.fromstring("1", (wide_for_real , high) , bitmap)
     im.save(sys.stdout , "PNG")

bye



More information about the Python-list mailing list