[Tutor] Parsing DICOMRT file

John Fouhy john at fouhy.net
Wed Dec 12 23:57:03 CET 2007


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

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

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!


More information about the Tutor mailing list