使用pydicom处理dcm文件提取信息并存储在csv
使用pydicom
使用pydicom
import pydicom as dicom
import csv
import codecs
import os
import re
DATA_PATH
= 'C:\\Users\\admin\Desktop\\level2\\'
list
=['PatientID',
'PatientAge',
'PatientSex',
'KVP',
'PixelSpacing',
'SliceThickness',
'Modality',
'Manufacturer',
'PatientName',
'XRayTubeCurrent',
'BodyPartExamined',
'Columns',
'Rows',
'WindowCenter',
'WindowWidth']
list1
=[]
flag1
= 0
def getfile
(DATA_PATH
):
global flag1
csvFile
= codecs.open
('D:\\1.csv',
'w', encoding
='GBK')
writer
= csv.writer
(csvFile
)
writer.writerow
(list
)
for root,dirs,files
in os.walk
(DATA_PATH
):
for file in files:
if 'dcm' in file:
filepath
=os.path.join
(root,file
)
ret
=getinfomation
(filepath
)
if flag1
==0:
writer.writerow
(list1
)
flag1
=1
writer.writerow
(ret
)
break
csvFile.close
()
def getinfomation
(path
):
information
= dicom.read_file
(path
)
print
(path
)
ds
=information
tmp
=[]
for i
in range
(0,len
(list
)):
try:
val
= str
(ds.data_element
(list
[i
]).value
)
except:
val
="null"
tmp.append
(str
(val
))
if flag1
==0:
list1.append
(re.match
('\(.*\)',str
(ds.data_element
(list
[i
]))).group
())
tmp.append
(str
(path
))
return tmp
if __name__
== '__main__':
getfile
(DATA_PATH
)
转载请注明原文地址:https://blackberry.8miu.com/read-17133.html