Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41124

Re: Reversing bits in a byte

Date 2013-03-12 08:20 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: Reversing bits in a byte
References <20130311153254.078484489A7@nhs-pd1e-esg101.ad1.nhs.net>
Newsgroups comp.lang.python
Message-ID <mailman.3231.1363097544.2939.python-list@python.org> (permalink)

Show all headers | View raw


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).




Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Reversing bits in a byte Tim Chase <python.list@tim.thechases.com> - 2013-03-12 08:20 -0500

csiph-web