[Tutor] Parsing DICOMRT file

Bryan Fodness bryan.fodness at gmail.com
Sat Mar 15 23:13:35 CET 2008

Haven't had a chance to look at this in a while.

On Wed, Dec 12, 2007 at 6:57 PM, John Fouhy <john at fouhy.net> wrote:

> On 13/12/2007, Bryan Fodness <bryan.fodness at gmail.com> wrote:
> > I am new to doing anything like this.  I have looked at
> > http://www.leadtools.com/SDK/Medical/DICOM/ltdc1.htm and am
> > not sure how to proceed.
> I haven't much experience here, but this is how I'd proceed, I think:
> 1. Start by reading the file.  It's binary data (I guess) so there's
> no point in reading lines.:
>  rawData = open('file.dcm', 'rb').read()
> 2. Write a function to parse the preamble:
>  def parsePreamble(data):
>    preamble = data[:128]
>    dicm = data[128:132]
>    # you might need to read up on encodings and things to make sure
> this test is valid
>    if dicm == 'DICM':
>      return preamble, 132
>    else:
>      raise NotAPreambleException

This satisfies the if statement.

> 3. Write functions to parse data elements.  The functions are going to
> try to parse a data element starting at a particular position, and if
> successful, return the position of the end of the element.
>  def parseDataelement(data, start):
>    # do stuff -- the web page you linked didn't have enough information
> here
>    return element, pos

I would like to extract 10-20 values from the file.

Starting at byte 132, the data elements are specified in the Explicit VR
little endian transfer syntax with a group number of 0002.  The data element
(0002, 0010) contains the Transfer Syntax UID, which specifies how the data
elements following the file meta information are encoded.  For this one, it
is 1.2.840.10008.1.2 which is equal  to LittleEndianImplicit.

where there is the 2-byte group number, a 2-byte element number,  a 4-byte
value length (VL) field, and a value field containing VL bytes.

Could someone help me get started.

I did an xml dump with another program and got,

     <element tag="300a,00c6" vr="CS" vm="1" len="6"

as an output example.

> 4. Parse the whole thing;
>  def parseDICOM(data):
>    elements = []
>    try:
>      preamble, next = parsePreamble(data)
>    except NotAPreambleException:
>      preamble, next = None, 0
>    while True:
>      element, next = parseDataElement(data, next)
>      elements.append(element)
>      # you will need some way of breaking out of this loop, either by
> checking the structure of
>      # element for an end condition, or by parseDataElement raising
> an exception.
>    return elements # and maybe preamble too if you want it
> HTH!

"The game of science can accurately be described as a never-ending insult to
human intelligence." - João Magueijo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080315/54c54961/attachment.htm 

More information about the Tutor mailing list