2016-08-15 49 views
1

我从超声设备创建了一个未压缩的dicom视频。现在我想在python应用程序中逐帧读取它,现在保存该文件。后来我想添加一些图像处理。到目前为止,我试图提取属于第一帧的字节。从字节数组中读取未压缩的图像

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)]) 

我该如何得到这个bytearray到一个文件(位图,jpeg什么)?我试图用image = Image.fromarray(byte_array)使用Python图像库,但得到一个错误。

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

我想我也有一个地方,我也必须指定图像的尺寸,但还没有弄清楚如何。

+1

我不知道很多关于蟒蛇,但我最近张贴处理类似的话题一个线程,也许你会发现有用:HTTP://stackoverflow.com/questions/37847414/viewing-dicom -image-with-bokeh/37936986#37936986 –

+1

尝试首先调整数组的大小以对应图像尺寸; http://stackoverflow.com/questions/7694772/turning-a-large-matrix-into-a-grayscale-image。在担心输出之前,我会转储帧行,列,字节大小和帧数以确认您的源。 –

回答

0

感谢评论我想出了如何解决它。图像是'RGB',阵列的形状是(3L,800L,376L)。我可以将pixel_array作为numpy数组并将其重塑为(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')