如何使用其他注解创建新的dicom图像?

时间:2019-03-25 23:39:00

标签: python pydicom

我想从一个创建两个pydicom文件。但是我无法以* .dcm格式保存带有注释的文件。

import pydicom
from pydicom.data import get_testdata_files
# read the dicom file
ds = pydicom.dcmread(test_image_fps[0])
# find the shape of your pixel data
shape = ds.pixel_array.shape
# get the half of the x dimension. For the y dimension use shape[0]
half_x = int(shape[1] / 2)
# slice the halves
# [first_axis, second_axis] so [:,:half_x] means slice all from first axis, slice 0 to half_x from second axis
data  = ds.pixel_array[:, :half_x]
print('The image has {} x {}'.format(data.shape[0],
                                        data.shape[1]))

# print the image information given in the dataset
print(data)
data.save_as("/my/path/after.dcm")
'numpy.ndarray' object has no attribute 'save_as

1 个答案:

答案 0 :(得分:0)

有关此信息,可以在pydicom documentation中找到。
注释“您的”;)代码:data = ds.pixel_array[:, :half_x]将ds.pixel_array的numpy.ndarray的视图分配给data。调用data.save_as()可能会失败,因为这是ds而不是data的属性。根据文档,您需要像这样写ds.PixelData属性:

ds.PixelData = data.tobytes() # where data is a numpy.ndarray or a view of an numpy.ndarray

# if the shape of your pixel data changes ds.Rows and ds.Columns must be updated,
# otherwise calls to ds.pixel_array.shape will fail
ds.Rows = 512 # update with correct number of rows
ds.Columns = 512 # update with the correct number of columns
ds.save_as("/my/path/after.dcm")