Ruby on Medicine: Converting DICOM to JPG

Share this article

first aid long shadow icon

This article will be the first article in the Ruby on Medicine (RoM) series focused on how Ruby can be applied to the medical domain.

In this tutorial, I will show you how to convert a DICOM file to a JPG image, which allows us to perform different operations on that image, which we will see in upcoming articles of this series.

The operating system I use for this tutorial is Ubuntu 14.10. If you are using another OS, don’t worry. I will refer you to the links that may be useful to use in the appropriate sections below.

What is DICOM?

Many of you are probably not too familiar with medical terms. That’s totally fine. Just to get you through this fray, let me start by introducing DICOM, the file format used as a starting point for this article.

I really liked this post by Roni, where he introduces DICOM (Digital Imaging and Communications in Medicine). I’ll use Roni’s post to briefly describe DICOM here.

Surely you’ve heard of terms like X-Ray, Ultrasound, CT, and MRI. Such medical images support DICOM and use it extensively. The core of the DICOM file specification is both a file format and a networking protocol.

All medical images are saved in such the DICOM format (i.e. dcm). This format not only includes images, but it also includes the patient’s information (i.e., name, gender, birth date), the equipment data (meaning, the device used), and other information.

The DICOM network protocol is used by medical applications connected to a hospital network to exchange information. It is also used to search and restore studies in the archive.

Of course, there is much more to DICOM, but it is beyond the scope of this article.

Why Convert DICOM to JPG?

Why convert a DICOM image to an image format like JPG? This are several potential reasons, some of which are:

  • DICOM files are known for their large sizes. Since we aim to work with the image part of the file only, converting to a JPG will both give an image and reduce file size. Obviously, this makes it much easier to share online.

  • As previously mentioned, DICOM files contain important data about patients. Thus, when transferring such files online, this sensitive information is exposed. This could be a violation of patient privacy. When we convert the DICOM file to an image, we are making the image anonymous, as the patient’s information is no longer there.

  • One of the aims of this series is to perform different image processing and computer vision tasks on medical images, like detecting a tumor, for instance. In these cases, the image part of the DICOM file serves the purpose. Almost anything can read or manipulate a JPG.

Setup

Let’s get started by making sure the environment has everything needed.

Ruby

If you don’t have Ruby installed, run the following command in the terminal to install Ruby:

sudo-apt get install ruby-full

The following command will display the version of Ruby version installed:

ruby -v

It should look something like this:

ruby 2.1.2p95 (2014-05-08) [x86_64-linux-gnu]

If you’re not on Ubuntu, install Ruby for your OS with the link below:

Ruby DICOM

Ruby DICOM is the library we will be using to convert DICOM to JPG.

From Ruby DICOM’s GitHub page:

Ruby DICOM is a small and simple library for handling DICOM in Ruby. DICOM (Digital Imaging and Communications in Medicine) is a standard for handling, storing, printing, and transmitting information in medical imaging. It includes a file format definition and a network communications protocol. Ruby DICOM supports reading from, editing and writing to this file format. It also features basic support for select network communication modalities like querying, moving, sending and receiving files.

To install Ruby DICOM, run the following command in the terminal:

  • Ubuntu: sudo gem install dicom
  • Windows: gem install dicom
  • Mac OS X: gem install dicom

RMagick

Viewing the converted image using Ruby, means it is time to install RMagick.

But, what is RMagick? Based on RMagick’s user guide:

RMagick is a binding from Ruby to the ImageMagickTM image manipulation library. ImageMagickTM is a free software suite to create, edit, and compose bitmap images. It can read, convert and write images in a large variety of formats. Images can be cropped, colors can be changed, various effects can be applied, images can be rotated and combined, and text, lines, polygons, ellipses and Bézier curves can be added to images and stretched and rotated.

RMagick is a complete interface to ImageMagick. Version 1.0.0 was released on February, 2003. Within its four major classes and 18 minor classes, RMagick defines over 650 methods and 350 constants. RMagick exploits Ruby idioms such as blocks and iterators, Struct classes, symbols, ?- and !-suffixed methods, and exceptions.

In order to install RMagick, run the following commands in your terminal:

Ubuntu:

sudo apt-get install imagemagick
sudo apt-get install libmagickwand-dev
sudo gem install rmagick

Links for other OS:

Viewing DICOM

With the environement ready to go, we need a DICOM file to call our very own. I have used the DIASTOLIX dataset (Alias Name) from here, and in particular, the file: IM-0004-0043.dcm

Viewing this DICOM file using a DICOM viewer like RadiAnt, we see something like the following:

imageDicomViewer

The following script will convert the DICOM file to a JPG image:

require 'dicom'
include DICOM

dcm = DObject.read("IM-0004-0044.dcm")
dcm_image = dcm.image
dcm_image.normalize.write("IM-0004-0044.jpg")

exit

Run this file using the syntax:

ruby file_name.rb

Which will output something like:

I, [2014-12-24T16:55:59.974029 #4056]  INFO -- DICOM: DICOM file successfully read: IM-0004-0044.dcm

And, a new JPG file should be created. Display the created JPG image using RMagick comes to the play.

require 'RMagick'
include Magick

img = ImageList.new("IM-0004-0044.jpg")
img.display

Putting it all together, the following script will convert DICOM to JPG and display the resulting image:

require 'RMagick'
require 'dicom'
include Magick
include DICOM

dcm = DObject.read("IM-0004-0044.dcm")
dcm_image = dcm.image
dcm_image.normalize.write("IM-0004-0044.jpg")

img = ImageList.new("IM-0004-0044.jpg")
img.display

exit

When you run the program, you’ll see the following output:

result

Now, you have a JPG image that can easily be used by different image processing operations. I’ll show you some of these operations in upcoming articles.

Before I wrap up this tutorial, I want to mention that you can convert the DICOM file to any image type you like (i.e. PNG, BMP). Just simply replace the .jpg part by the image file with your extension of choice.

Are there any specific topics you would like to see in this series? If so, leave them in the comments below.

Frequently Asked Questions (FAQs) on Converting DICOM to JPG

What is the DICOM format and why would I need to convert it to JPG?

DICOM, which stands for Digital Imaging and Communications in Medicine, is a standard format used to store and transmit medical images. It’s widely used in the healthcare industry, particularly in radiology. However, DICOM files can be difficult to open and view without specialized software. Converting DICOM to JPG, a more universally accessible format, allows for easier viewing and sharing of these images.

Can I convert DICOM files to JPG without losing image quality?

Yes, it’s possible to convert DICOM files to JPG without significant loss of image quality. However, it’s important to note that JPG is a lossy format, which means some data might be lost during the conversion. The key is to use a reliable conversion tool that maintains as much of the original image quality as possible.

Are there any free online tools for converting DICOM to JPG?

Yes, there are several free online tools that can convert DICOM files to JPG. These include Convertio, Aspose, OnlineConvertFree, and Media.io. However, keep in mind that while these tools are convenient, they may not offer the same level of image quality or security as a dedicated software program.

How can I convert DICOM to JPG using Ruby?

Ruby is a powerful programming language that can be used to convert DICOM to JPG. This involves using the ‘dicom’ gem, which is a library specifically designed for handling DICOM files. You’ll need to install the gem, write a script to load the DICOM file, and then save it as a JPG.

Is it safe to use online converters for medical images?

While online converters are convenient, they may not provide the level of security necessary for handling sensitive medical images. If you’re dealing with patient data, it’s crucial to ensure that the converter you’re using complies with all relevant privacy and security regulations.

Can I batch convert DICOM files to JPG?

Yes, many conversion tools, both online and offline, offer the option to batch convert files. This can save a significant amount of time if you’re dealing with a large number of DICOM files.

What should I consider when choosing a DICOM to JPG converter?

When choosing a converter, consider factors such as ease of use, conversion quality, security, and whether it supports batch conversion. If you’re dealing with sensitive medical data, also consider whether the tool complies with healthcare regulations.

Can I convert DICOM to other image formats besides JPG?

Yes, DICOM files can be converted to a variety of other image formats, including PNG, TIFF, and BMP. The exact formats supported will depend on the conversion tool you’re using.

Why are some of my DICOM files not converting correctly?

If you’re having trouble converting DICOM files, it could be due to a number of reasons. The files might be corrupted, or they might be in a format that your conversion tool doesn’t support. If you’re using a programming language like Ruby, there might be an issue with your code.

How can I view DICOM files without converting them?

To view DICOM files without converting them, you’ll need a DICOM viewer. There are many free and paid DICOM viewers available, both online and as downloadable software. These tools can open and display DICOM files, and often offer additional features such as image manipulation and annotation.

Abder-Rahman AliAbder-Rahman Ali
View Author

Doctor and author focussed on leveraging machine/deep learning and image processing in medical image analysis.

GlennG
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week