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.
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')