Pydicom: how to convert dicom to jpeg

Created on 11 May 2017  Â·  8Comments  Â·  Source: pydicom/pydicom

what i want is to get a jpeg file for each dcm file ?is there any best practices of pydicom ,i find nothing looking over documentation

question

Most helpful comment

This doesn't work for me, so I'm guessing it doesn't work for others. I'm including some code that converts a dicom file into a png for the next person who ends up here via google :-)

import numpy as np
import png
import pydicom

ds = pydicom.dcmread(path)

shape = ds.pixel_array.shape

# Convert to float to avoid overflow or underflow losses.
image_2d = ds.pixel_array.astype(float)

# Rescaling grey scale between 0-255
image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 255.0

# Convert to uint
image_2d_scaled = np.uint8(image_2d_scaled)

# Write the PNG file
with open(destination, 'wb') as png_file:
    w = png.Writer(shape[1], shape[0], greyscale=True)
    w.write(png_file, image_2d_scaled)

All 8 comments

Not tested, but you should be able to use pillow and numpy:

from PIL.Image import fromarray

ds = read_file('file-in.dcm')
im = fromarray(ds.pixel_array)
im.save('file-out.jpg')

You will likely have to do some work to ensure you have the image data in a suitable format prior to saving.

I think this can be closed as answered.

This doesn't work for me, so I'm guessing it doesn't work for others. I'm including some code that converts a dicom file into a png for the next person who ends up here via google :-)

import numpy as np
import png
import pydicom

ds = pydicom.dcmread(path)

shape = ds.pixel_array.shape

# Convert to float to avoid overflow or underflow losses.
image_2d = ds.pixel_array.astype(float)

# Rescaling grey scale between 0-255
image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 255.0

# Convert to uint
image_2d_scaled = np.uint8(image_2d_scaled)

# Write the PNG file
with open(destination, 'wb') as png_file:
    w = png.Writer(shape[1], shape[0], greyscale=True)
    w.write(png_file, image_2d_scaled)

@tedivm wonderful

Thanks @tedivm. Wrapped your code into a function for convenient copy-paste

import numpy as np
import png, os, pydicom

source_folder = r'path\to\source'
output_folder = r'path\to\output\folder'


def dicom2png(source_folder, output_folder):
    list_of_files = os.listdir(source_folder)
    for file in list_of_files:
        try:
            ds = pydicom.dcmread(os.path.join(source_folder,file))
            shape = ds.pixel_array.shape

            # Convert to float to avoid overflow or underflow losses.
            image_2d = ds.pixel_array.astype(float)

            # Rescaling grey scale between 0-255
            image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 255.0

            # Convert to uint
            image_2d_scaled = np.uint8(image_2d_scaled)

            # Write the PNG file
            with open(os.path.join(output_folder,file)+'.png' , 'wb') as png_file:
                w = png.Writer(shape[1], shape[0], greyscale=True)
                w.write(png_file, image_2d_scaled)
        except:
            print('Could not convert: ', file)


dicom2png(source_folder, output_folder)

@rohit-patel - you forgot to import os ;-)

This doesn't work for me, so I'm guessing it doesn't work for others. I'm including some code that converts a dicom file into a png for the next person who ends up here via google :-)

import numpy as np
import png
import pydicom

ds = pydicom.dcmread(path)

shape = ds.pixel_array.shape

# Convert to float to avoid overflow or underflow losses.
image_2d = ds.pixel_array.astype(float)

# Rescaling grey scale between 0-255
image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 255.0

# Convert to uint
image_2d_scaled = np.uint8(image_2d_scaled)

# Write the PNG file
with open(destination, 'wb') as png_file:
    w = png.Writer(shape[1], shape[0], greyscale=True)
    w.write(png_file, image_2d_scaled)

Only solution that worked for my problem, thank you!

This doesn't work for me, so I'm guessing it doesn't work for others. I'm including some code that converts a dicom file into a png for the next person who ends up here via google :-)

import numpy as np
import png
import pydicom

ds = pydicom.dcmread(path)

shape = ds.pixel_array.shape

# Convert to float to avoid overflow or underflow losses.
image_2d = ds.pixel_array.astype(float)

# Rescaling grey scale between 0-255
image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 255.0

# Convert to uint
image_2d_scaled = np.uint8(image_2d_scaled)

# Write the PNG file
with open(destination, 'wb') as png_file:
    w = png.Writer(shape[1], shape[0], greyscale=True)
    w.write(png_file, image_2d_scaled)

@tedivm
got an error, maybe it can be optimized?

Traceback (most recent call last):
  File "D:/ml/untitled/dcm2png.py", line 81, in <module>
    dcm2png(file,'png2\\'+file+'.png')
  File "D:/ml/untitled/dcm2png.py", line 16, in dcm2png
    shape = ds.pixel_array.shape
  File "D:\ProgramData\Anaconda3\envs\py36\lib\site-packages\pydicom\dataset.py", line 778, in __getattr__
    return object.__getattribute__(self, name)
  File "D:\ProgramData\Anaconda3\envs\py36\lib\site-packages\pydicom\dataset.py", line 1615, in pixel_array
    self.convert_pixel_data()
  File "D:\ProgramData\Anaconda3\envs\py36\lib\site-packages\pydicom\dataset.py", line 1324, in convert_pixel_data
    self._convert_pixel_data_without_handler()
  File "D:\ProgramData\Anaconda3\envs\py36\lib\site-packages\pydicom\dataset.py", line 1434, in _convert_pixel_data_without_handler
    raise last_exception
  File "D:\ProgramData\Anaconda3\envs\py36\lib\site-packages\pydicom\dataset.py", line 1414, in _convert_pixel_data_without_handler
    self._do_pixel_data_conversion(handler)
  File "D:\ProgramData\Anaconda3\envs\py36\lib\site-packages\pydicom\dataset.py", line 1441, in _do_pixel_data_conversion
    arr = handler.get_pixeldata(self)
  File "D:\ProgramData\Anaconda3\envs\py36\lib\site-packages\pydicom\pixel_data_handlers\numpy_handler.py", line 263, in get_pixeldata
    "Unable to convert the pixel data: one of Pixel Data, Float "
AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset
Was this page helpful?
0 / 5 - 0 ratings