Reversing bits in a byte

Robert Flintham Robert.Flintham at uhb.nhs.uk
Tue Mar 12 09:31:28 EDT 2013


Further to my earlier reply to Dave:

I'd like to either display the image in a GUI, or save it in a format that can be opened easily in Windows (like a PNG or a 24-bit BMP).

I know the dimensions as it's coming from the header of a DICOM file.  I'm trying to analyse DICOM images where an 'overlay' image is stored as a bitmap in the header information.  So the bitmap data is one DICOM tag (6000,3000) and the height and width of the overlay are in two other tags (6000,0010) and (6000,0011).

All the best,
Rob

Robert Flintham
Trainee Clinical Scientist - MRI

Tel:      +44 (0)121 371 7000
Email:       Robert.Flintham at uhb.nhs.uk
Web:      http://www.uhb.nhs.uk

We're bringing the world's most advanced cancer treatments to Birmingham.
Find out more at www.qecancerappeal.org or text QEHB01 £5 to 70070 to donate £5 to our appeal.

RRPPS
Medical Physics - University Hospitals Birmingham NHS Foundation Trust
63 Melchett Road, Kings Norton,
Birmingham, B30 3HP


ð Delivering the best in care



-----Original Message-----
From: Tim Chase [mailto:python.list at tim.thechases.com] 
Sent: 12 March 2013 13:21
To: Robert Flintham
Cc: 'python-list at python.org'
Subject: Re: Reversing bits in a byte

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






DISCLAIMER:
This email and any attachments hereto contains proprietary information, some or all of which may be confidential or legally privileged. It is for the exclusive use of the intended recipient(s) only. If an addressing or transmission error has misdirected this e-mail and you are not the intended recipient(s), please notify the author by replying to this e-mail. If you are not the intended recipient you must not use, disclose, distribute, copy, print, or rely on this e-mail or any attachments, as this may be unlawful.




More information about the Python-list mailing list