How To Extract a JPG or PNG from a DICOM Image
Introduction
A DICOM Image is a container, which combines a header (metadata like patient ID, study date, etc) with pixel data (i.e. the image itself). The latter may be uncompressed (i.e. bitmap) or compressed with one of the many codecs suported by the DICOM standard, such as JPEG-LS or High-Throughput JPEG 2000 (HTJ2K).
To process pixels, it may be easier to extract the pixel data into a simple JPG or PNG file. Two methods are described here: 1) DICOMweb and 2) Command-line utilities. Code/scripting using a DICOM library would also work, but that's out of scope for this article.
If you'd like to dig deeper into this topic, see the following links into the DICOM standard:
Note: Not all DICOM objects contain pixel data. Some are purely metadata, like Structured Reports (SR) and Key Object Selections (KOS).
1. Extracting JPG/PNG Via DICOMweb
Check your server's DICOM conformance statement to ensure it supports the /rendered
call in WADO-RS. See the DICOM standard for more details. We use Orthanc and know that it supports this call.
If your server supports this endpoint, then the rest is easy! You can just set a GET
call using the following URL format (replace the UIDs in curly brackets with the values you're working with):
/studies/{Study Instance UID}/series/{Series Instance UID}/instances/{SOP Instance UID}/rendered
and add an Accept
header with either image/jpeg
or image/png
depending on what you want, and that should be all!
DICOMweb Example
Here's a working example from the SIIM Hackathon Server. It required a valid hackathon API key:
https://hackathon.siim.org/dicomweb/studies/1.3.6.1.4.1.14519.5.2.1.6279.6001.300027087262813745730072134723/series/1.3.6.1.4.1.14519.5.2.1.6279.6001.513114548408601984123939083099/instances/1.3.6.1.4.1.14519.5.2.1.6279.6001.197993217821785409800235232773/rendered
By default, Orthanc returns a JPEG image, but you can change that with:
Accept: image/png
You can try this via tools like Postman, Insomnia, etc.
2. Extracting JPG/PNG Via Command-Line Interface (CLI) Utilities
For this, we'll be using the excellent dcm4che CLI toolkit. This toolkit works on Windows, Mac and Linux (even headless servers). You can download the latest and greatest verion of dcm4che here (you'll want to download the file ending with -bin.zip
).
Once downloaded, you can run dcm2jpg
and point it to a DICOM file to export the pixel data like so:
./dcm4che-5.23.2/bin/dcm2jpg /path/to/dicom-file.dcm
Find more information on dcm2jpg here or by running dcm2jpg --help
Note: Other DICOM toolkits have support for this as well. See the dcmtk equivalent.
CLI Example
Assuming you have both dcm4che and this sample hackathon image downloaded to the current folder (and dcm4che unzipped). You can run the following to export to JPEG:
./dcm4che-5.23.2/bin/dcm2jpg IM-0031-0001.dcm IM-0031-0001.jpg
Or, the following to go with PNG instead:
./dcm4che-5.23.2/bin/dcm2jpg -F PNG IM-0031-0001.dcm IM-0031-0001.jpg