1

I created a uncompressed dicom video from an ultrasound device. Now I want to read it frame by frame in a python application and for now save the file. Later I want to add some image processing. So far I've tried to extract the bytes belonging to the first frame.

import dicom
import array
from PIL import Image

filename = 'image.dcm'
img = dicom.read_file(filename)
byte_array = array.array('B', img.PixelData[0:(img.Rows * img.Columns)])

How do I get now this bytearray into a file (bitmap, jpeg whatever)? I've tried using the python image library with image = Image.fromarray(byte_array) but got an error.

AttributeError: 'str' object has no attribute 'array_interface'

I guess somewhere I also have to specify the dimensions of the image but haven't figured out how.

ipa
  • 1,496
  • 1
  • 17
  • 36
  • 1
    I don't know much about python, but I had recently posted to a thread dealing with a similar topic, maybe you find that useful:http://stackoverflow.com/questions/37847414/viewing-dicom-image-with-bokeh/37936986#37936986 – Markus Sabin Aug 16 '16 at 06:22
  • 1
    Try resizing the array first to correspond to the image dimensions; http://stackoverflow.com/questions/7694772/turning-a-large-matrix-into-a-grayscale-image. I would dump the frame row, cols, byte size and frame count to confirm your source before worrying about the output. – john elemans Aug 16 '16 at 16:14

1 Answers1

0

Thanks to the comments I figured out how to solve it. The Image was in 'RGB' and the shape of the array was (3L, 800L, 376L). Instead of converting it to a byte array I can take the pixel_array as numpy array and reshape it to (800L, 376L, 3L).

import dicom
from PIL import Image

filename = 'image.dcm'
img = dicom.read_file(filename)
output = img.pixel_array.reshape((img.Rows, img.Columns, 3))
image = Image.fromarray(output).convert('LA')
image.save('output.png')
ipa
  • 1,496
  • 1
  • 17
  • 36