Converting DCM Images from Ultrasound to JPEGs

My wife had an ultrasound where they took lots and lots of pictures of our soon-to-be baby. They provided a few printouts. That’s nice and all, but I asked if I could get the images on CD. The radiologists said I could, and they gave me a CD a few days later. The problem is that they gave me this goofy CD with all sorts of disclaimers on it. You need Windows, Internet Explorer, blah blah blah. In particular, I like the disclaimer: “Not supported: Macintosh.”

What the heck, people!? These are freaking images! There is no operating system dependency on images! Read on to see how I solved this. My solution is for Macintosh or UNIX users who have ImageMagick and are not afraid of a command line. If you’re a Windows user, just run along and play with your cute little image viewer. There’s a good sheep. Just run whatever they give you. That’s good”

It turns out that they use an image format (DICOM) that is common in the medical industry, but not common in your average household. So, rather than convert the original DICOM (.dcm) images to a consumer-friendly format (e.g. TIFF or JPEG), they go to all manner of expensive development and licensing to produce some crappy Windows DICOM Image viewer. Just what I need: images that I can only possibly read using their oddball software.

How hard would it be to create a pile of .html files where all the interesting metadata (that’s what DICOM is good for) is printed in the HTML, and then the images are stored as JPEGs and referenced in the HTML? It would be a heck of a lot cheaper than developing (or licensing) some DICOM reader and making people install it on their computers. Plus, there is a possibility (however small) that the DICOM reader software has a vulnerability, has a virus attached, or some other undesirable problem. On the other hand, straight data like HTML and JPEG cannot easily harbor such malware, and they take less room on the disc as well.

The Method

Now that I’ve ranted a while, here’s how I converted them. I found a pile of files that were named like 1.3.12.2.1107.5.5.7213.51021.102239.247.dcm. First off, I copied them all onto my hard disk. They all had a common prefix of 1.3.12.2.1107.5.5.7213.51021.1 so I renamed them all:

for i in 1.3.12.2.1107.5.5.7213.51021.1*
do
mv "$i" "Ultrasound${i#1.3.12.2.1107.5.5.7213.51021.1*}"
done

Next I converted them all to JPEG. There’s really not much to this. The convert command comes from ImageMagick.

for i in *.dcm
do
convert "$i" "${i%.dcm}.jpg"
echo $i
done

Lastly, I wanted to put some reasonable EXIF data into these JPEG files. The images clearly indicated they were from a SIEMENS Ultrasound system of one kind or another (I really don’t care what kind), and the date is also embedded in the image metadata. Using exiftool, a perl-based tool that adds and modifies EXIF data, I was able to create EXIF tags where none existed before. I used a hackish shell script to extract the metadata into one file I call script.sh and then I run script.sh.

for i in *.dcm
do
  echo -n "exiftool " >> script.sh
  time=$(strings "$i" | head -15 | tail -1 | cut -d \. -f 1 |
    perl -pe 's,(..)(..)(..),$1:$2:$3,g')
  echo -n "-Make=SIEMENS -Model=Ultrasound -DateTimeOriginal=\"2005:10:21 ${time}\"" >> script.sh
  echo " ${i%.dcm}.jpg" >> script.sh
done

sh script.sh

Since I knew that all my images were from October 21, 2005, I just hardcoded that in. It’s possible to extract that data, too. Note that this is a total hack. Basically, the time of the image capture was the 15th line in the output of strings when run on these files. That may not be true (probably won’t be true) for other DCM files.

If you’re tackling some of these issues yourself, I hope my solution gives you a leg up on solving it yourself.