-
Notifications
You must be signed in to change notification settings - Fork 184
/
FixCommaBug.py
77 lines (65 loc) · 1.84 KB
/
FixCommaBug.py
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
69
70
71
72
73
74
75
76
77
############################################################################
#
# Program: GDCM (Grassroots DICOM). A DICOM library
#
# Copyright (c) 2006-2011 Mathieu Malaterre
# All rights reserved.
# See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the above copyright notice for more information.
#
############################################################################
"""
Using LC_NUMERIC set to something not compatible with "C" it is possible to write out "," instead of
"." as required by the DICOM standard
Issue is still current (IMHO) with gdcm 2.0.9
"""
import gdcm
import sys
filename = sys.argv[1]
outname = sys.argv[2]
# read
r = gdcm.Reader()
r.SetFileName( filename )
if not r.Read():
print "not valid"
sys.exit(1)
file = r.GetFile()
dataset = file.GetDataSet()
ano = gdcm.Anonymizer()
ano.SetFile( file )
tags = [
gdcm.Tag(0x0018,0x1164),
gdcm.Tag(0x0018,0x0088),
gdcm.Tag(0x0018,0x0050),
gdcm.Tag(0x0028,0x0030),
]
for tag in tags:
print tag
if dataset.FindDataElement( tag ):
pixelspacing = dataset.GetDataElement( tag )
#print pixelspacing
bv = pixelspacing.GetByteValue()
str = bv.GetBuffer()
#print bv.GetLength()
#print len(str)
new_str = str.replace(",",".")
# Need to explicitly pass bv.GetLength() to remove any trailing garbage
ano.Replace( tag, new_str, bv.GetLength() )
#print dataset
w = gdcm.Writer()
w.SetFile( file )
w.SetFileName( outname )
if not w.Write():
print "Cannot write"
sys.exit(1)
# paranoid:
image_reader = gdcm.ImageReader()
image_reader.SetFileName( outname )
if not image_reader.Read():
print "there is still a comma"
sys.exit(1)
print "Success!"
sys.exit(0) # success