我学习如何将 DCM 文件转换为原始文件。从 Git Hub 获取代码:
https
://github.com/xiasun/dicom2raw/blob/master/dicom2raw.py
并且出现错误“无法转换‘字节’在
“allInOne += dataset.PixelData”行中隐含地对象到str ”
我尝试使用“encode(“utf-8”)”,但它使allInOne为空。顺便问一下,有没有代码可以生成.raw文件对应的.mhd文件?
import dicom
import os
import numpy
import sys
dicomPath = "C:/DataLuna16pen/dcmdata/"
lstFilesDCM = [] # create an empty list
for dirName, subdirList, fileList in os.walk(dicomPath):
allInOne = ""
print(subdirList)
i=0
for filename in fileList:
i+=1
if "".join(filename).endswith((".dcm", ".DCM")):
path = dicomPath + "".join(filename)
dataset = dicom.read_file(path)
for n,val in enumerate(dataset.pixel_array.flat):
dataset.pixel_array.flat[n] = val / 60
if val < 0:
dataset.pixel_array.flat[n] = 0
dataset.PixelData = numpy.uint8(dataset.pixel_array).tostring()
allInOne += dataset.PixelData
print ("slice " + "".join(filename) + " done ",end=" ")
print (i)
newFile = open("./all_in_one.raw", "wb")
newFile.write(allInOne)
newFile.close()
print ("RAW file generated")