GDCM  2.4.5
FixCommaBug.py
1 ############################################################################
2 #
3 # Program: GDCM (Grassroots DICOM). A DICOM library
4 #
5 # Copyright (c) 2006-2011 Mathieu Malaterre
6 # All rights reserved.
7 # See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8 #
9 # This software is distributed WITHOUT ANY WARRANTY; without even
10 # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 # PURPOSE. See the above copyright notice for more information.
12 #
13 ############################################################################
14 
15 """
16 Using LC_NUMERIC set to something not compatible with "C" it is possible to write out "," instead of
17 "." as required by the DICOM standard
18 Issue is still current (IMHO) with gdcm 2.0.9
19 """
20 
21 import gdcm
22 import sys
23 
24 filename = sys.argv[1]
25 outname = sys.argv[2]
26 
27 # read
28 r = gdcm.Reader()
29 r.SetFileName( filename )
30 if not r.Read():
31  print "not valid"
32  sys.exit(1)
33 
34 file = r.GetFile()
35 dataset = file.GetDataSet()
36 
37 ano = gdcm.Anonymizer()
38 ano.SetFile( file )
39 
40 tags = [
41 gdcm.Tag(0x0018,0x1164),
42 gdcm.Tag(0x0018,0x0088),
43 gdcm.Tag(0x0018,0x0050),
44 gdcm.Tag(0x0028,0x0030),
45 ]
46 
47 for tag in tags:
48  print tag
49  if dataset.FindDataElement( tag ):
50  pixelspacing = dataset.GetDataElement( tag )
51  #print pixelspacing
52  bv = pixelspacing.GetByteValue()
53  str = bv.GetBuffer()
54  #print bv.GetLength()
55  #print len(str)
56  new_str = str.replace(",",".")
57  # Need to explicitly pass bv.GetLength() to remove any trailing garbage
58  ano.Replace( tag, new_str, bv.GetLength() )
59 
60 #print dataset
61 
62 w = gdcm.Writer()
63 w.SetFile( file )
64 w.SetFileName( outname )
65 if not w.Write():
66  print "Cannot write"
67  sys.exit(1)
68 
69 # paranoid:
70 image_reader = gdcm.ImageReader()
71 image_reader.SetFileName( outname )
72 if not image_reader.Read():
73  print "there is still a comma"
74  sys.exit(1)
75 
76 print "Sucess!"
77 sys.exit(0) # success

Generated on Fri Sep 25 2015 17:58:20 for GDCM by doxygen 1.8.9.1
SourceForge.net Logo