How to read and convert .dcm File to .png,.jpg,.gif

richieking 0 Tallied Votes 4K Views Share

This is one of the easiest steps to view and convert dcm image useing the snake(python)

There are 3 modules involved.

1. Search for pydicom and install
2. Search for matplotlib and install
3. Search for pylab and install.

If you are on ubuntu destro, Installing matplotlib via the repo will pull include the pylab. That is as far as i know. Maybe Pylab is included in matplotlib module. I stand for corrections .

Note To convert to .png,.gif,.jpg file ...

Pylab will popup the file and it will come with image as required.
Look at the statusbar and there are buttons with save disket image. Click on it then select your choice of file type to save. Make sure you save it where easy to access.

Thank you. :)

import dicom
import Image
import pylab

dFile=dicom.read_file("/home/richie/d1.dcm") #path to file
pylab.imshow(dFile.pixel_array,cmap=pylab.cm.bone) # pylab readings and conversion
pylab.show() #Dispaly

Dani AI

Generated

If you want a direct, scriptable conversion (no GUI), read the DICOM with pydicom, apply the Modality LUT/Rescale and VOI LUT so the pixels are correctly windowed, handle MONOCHROME1 inversion, then save with Pillow. This preserves clinical intent and avoids the common “too bright/too dark” pitfall. The helpers apply_modality_lut and apply_voi_lut implement the standard pipeline, and MONOCHROME1 means low pixel values should display as white (so invert after VOI). PNG supports up to 16 bits per channel; JPEG/GIF are 8-bit. Convert accordingly. pydicom pixel processing, apply_voi_lut, DICOM Photometric Interpretation, Pillow Image.fromarray, PNG 16-bit note.

import pydicom, numpy as np
from pydicom.pixel_data_handlers.util import apply_modality_lut, apply_voi_lut
from PIL import Image

ds = pydicom.dcmread("input.dcm")
arr = ds.pixel_array                                  # requires a decompressor if compressed
arr = apply_modality_lut(arr, ds)                     # rescale / modality LUT
arr = apply_voi_lut(arr, ds)                          # window or VOI LUT

if ds.PhotometricInterpretation == "MONOCHROME1":     # invert after VOI
    arr = np.max(arr) - arr

def to_uint8(a):
    a = a.astype(np.float32); a -= a.min()
    return (a / (a.max() or 1) * 255).astype(np.uint8)

Image.fromarray(arr.astype(np.uint16), mode="I;16").save("out.png")     # 16-bit PNG
Image.fromarray(to_uint8(arr), mode="L").save("out.jpg", quality=95)    # 8-bit JPEG
Image.fromarray(to_uint8(arr), mode="L").save("out.gif")                # 8-bit GIF

Troubleshooting: if ds.pixel_array raises about compressed Transfer Syntax, install a decoder such as pylibjpeg or gdcm (pydicom will use them automatically). Compressed pixel data guide.

richieking 44 Master Poster

I included image module but its not active in the code.

That means i am working on image module's version.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.