Reversing bits in a byte

Tim Chase python.list at tim.thechases.com
Tue Mar 12 14:20:54 CET 2013


On 2013-03-11 15:32, Robert Flintham wrote:
> I have a 'bytes' object which contains a simple bitmap image (i.e.
> 1 bit per pixel).  I can't work out how I would go about displaying
> this image.  Does anyone have any thoughts?

You'd need to detail
- how you want to display it (console, GUI, web page)
- how you know what the dimensions are
- the bit order

It could be something as simple as

  HEIGHT = 40
  some_bytes = file('data.bin').read()
  WIDTH = len(some_bytes) // HEIGHT
  for i, byte in enumerate(some_bytes):
    if i and i % WIDTH == 0:
      print # a new line
    for bit in range(8):
      if byte & (1 << bit):
        print '*',
      else:
        print ' ',


-tkc

> DISCLAIMER:
[trim a paragraph of useless junk]
Please remove these disclaimers if at all possible.  You're posting
to a public forum, which pretty much waives all credibility to the
disclaimer (not that they've held much legal standing in any argument
I've heard).








More information about the Python-list mailing list