raster (PIL)

Diez B. Roggisch deets at nospam.web.de
Thu Jul 23 09:27:48 EDT 2009


superpollo wrote:

> 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

No. Because that would skip up to 7 bits.

Instead, do

bytes_in_a_row = wide/bits_in_a_byte
if wide % bits_in_a_byte:
   bytes_in_a_row += 1

Diez



More information about the Python-list mailing list