1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| from pydicom import dcmread
from tkinter import filedialog
import csv
import os
path = filedialog.askopenfilename(initialdir="c:\\",
title="Select a File")
path1 = path.split('/')
nom_fichier = path1[len(path1)-1]
outputfile = ('temp.txt')
# utilise la lib pydicom pour lire et enregistrer le dcm dans un fichier txt
di = dcmread(path)
with open(outputfile, 'w') as f:
f.write(str(di))
# récupére les tag dicom intéressant
with open(outputfile, "r") as f:
temporaire = []
temp = []
i = 0
for ligne in f:
ligne = ligne.lstrip()
if (ligne[:12] == "(0008, 0104)" or ligne[:12] == "(0008, 0100)"
or ligne[:12] == "(0040, a30a)"):
ligne_splited = ligne.split("'")
temp.append(ligne_splited[1])
if ligne[:12] == "(0040, a30a)":
# tagdicom du groupe de la mesure
if temp[i-6] == '121410':
temporaire.append('')
else:
temporaire.append(temp[i-6])
# nom groupe de la mesure
if temp[i-5] == 'User chosen value':
temporaire.append('')
else:
temporaire.append(temp[i-5])
# tag dicom de la mesure
temporaire.append(temp[i-4])
# nom de la mesure
temporaire.append(temp[i-3])
# valeur mesurée
temporaire.append(temp[i])
# unité de mesure
if temp[i-1] == 'no units':
temporaire.append('')
else:
temporaire.append(temp[i-1])
i += 1
# transforme en un CSV
nom_fichier = nom_fichier + '.csv'
with open(nom_fichier, 'w') as out_file:
writer = csv.writer(out_file)
writer.writerow(('code DICOM paramètre', 'Nom paramètre',
'Code DICOM mesure', 'Valeur mesurée',
'Mesure', 'Unité mesure'))
for i in range(0, len(temporaire), 6):
writer.writerow((temporaire[i],
temporaire[i+1],
temporaire[i+2],
temporaire[i+3],
temporaire[i+4],
temporaire[i+5]))
os.remove(outputfile) |
Partager