0

我想从一个创建两个 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
4

1 回答 1

0

这方面的信息可以在pydicom 文档中找到。
备注“你的” ;) 代码:data = ds.pixel_array[:, :half_x]将 ds.pixel_array 的 numpy.ndarray 视图分配给data. 调用data.save_as()预期会失败,因为那是dsnot的一个属性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")   
于 2019-03-27T12:56:20.117 回答