Newbie looking for elegant solution

kai.peters at gmail.com kai.peters at gmail.com
Wed Mar 25 20:38:36 EDT 2015


On Tuesday, 24 March 2015 20:14:06 UTC-7, otaksoft... at gmail.com  wrote:
> I have a list containing 9600 integer elements - each integer is either 0 or 1.
> 
> Starting at the front of the list, I need to combine 8 list elements into 1 by treating them as if they were bits of one byte with 1 and 0 denoting bit on/off (the 8th element would be the rightmost bit of the first byte).
> 
> The end result should be a new list that is 8 x shorter than the original list containing integers between 0 and 255.
> 
> Speed is not of utmost importance - an elegant solution is. Any suggestions?
> 
> Thanks for all input,
> Kai

I though that the bytes type is Python 3 only? If so, I cannot use it.

Using PILLOW, I am generating images from text and these are then sent to a black & white image rendering device which expects 8 one bit pixels as one byte (as a condensed format I guess). This device then "turns" bits into pixels again and displays the image that way.

If that is clear enough for you to suggest a better way to achieve what I am after, I'd like to hear it.

Kai



More information about the Python-list mailing list