Converting DICOM xray images to JPEG

I recently needed to convert a CD of xrays into jpeg images, and it wasn’t immediately obvious how to do this. The files on the CD were in DICOM format , which appears to be a commonly used format in the medical community that includes the image and extra metadata about the patient. I first used the aeskulap viewer which worked fine for viewing, but has no options (that I could find) for exporting them to regular jpeg images. It is a pretty nice viewer, allowing you to load the DICOMDIR file which will load all the images and patient data into the viewer.

There are a variety of tools available in Fedora when you search for DICOM but it wasn’t obvious which ones could handle conversions, and ImageMagick didn’t recognize the format. I finally discovered that the dcmj2pnm tool, part of the DCMTK project could convert to a variety of image formats, including pgm/ppm, png, tiff, jpeg, and bmp.

You can examine the DICOM image using the --image-info command:

[xrays]$ dcmj2pnm --image-info --no-output --verbose ST1-SE1.dcm

I: reading DICOM file: ST1-SE1.dcm
I: preparing pixel data
I: dumping image parameters
I:   filename            : ST1-SE1.dcm
I:   transfer syntax     : JPEG Lossless, Non-hierarchical, 1st Order Prediction
I:   SOP class           : ComputedRadiographyImageStorage
I:   SOP instance UID    : 1.2.410.200028.100.3.20180514.1719220880.22614.1.3
I:   columns x rows      : 2108 x 1492
I:   bits per sample     : 14
I:   color model         : MONOCHROME2
I:   pixel aspect ratio  : 1.00
I:   number of frames    : 1 (1 processed)
I:   VOI LUT function    : <default>
I:   VOI windows in file : 1
I:   - <no explanation>
I:   VOI LUTs in file    : 0
I:   presentation shape  : <default>
I:   overlays in file    : 0
I:   maximum pixel value : 15204
I:   minimum pixel value : 0
I: cleaning up memory

The images I have are only single frame, but from the manpage for dcmj2pnm it looks like there could be multiple frames per image. There are also quite a number of other options available that aren’t needed for basic image conversion.

Converting the DICOM to jpeg ends up being really easy:

[xrays]$ dcmj2pnm --write-jpeg ST1-SE1.dcm foot-xray.jpg

Which produces an image like this:

Foot xray