GDCM: FixCommaBug.py

GDCM 3.2.5

FixCommaBug.py
1
14
15"""
16Using LC_NUMERIC set to something not compatible with "C" it is possible to write out "," instead of
17"." as required by the DICOM standard
18Issue is still current (IMHO) with gdcm 2.0.9
19"""
20
21import gdcm
22import sys
23
24filename = sys.argv[1]
25outname = sys.argv[2]
26
27# read
28r = gdcm.Reader()
29r.SetFileName( filename )
30if not r.Read():
31 print "not valid"
32 sys.exit(1)
33
34file = r.GetFile()
35dataset = file.GetDataSet()
36
37ano = gdcm.Anonymizer()
38ano.SetFile( file )
39
40tags = [
41 gdcm.Tag(0x0018,0x1164),
42 gdcm.Tag(0x0018,0x0088),
43 gdcm.Tag(0x0018,0x0050),
44 gdcm.Tag(0x0028,0x0030),
45]
46
47for 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
62w = gdcm.Writer()
63w.SetFile( file )
64w.SetFileName( outname )
65if not w.Write():
66 print "Cannot write"
67 sys.exit(1)
68
69# paranoid:
70image_reader = gdcm.ImageReader()
71image_reader.SetFileName( outname )
72if not image_reader.Read():
73 print "there is still a comma"
74 sys.exit(1)
75
76print "Success!"
77sys.exit(0) # success
gdcm::Anonymizer
Anonymizer.
Definition gdcmAnonymizer.h:78
gdcm::ImageReader
ImageReader.
Definition gdcmImageReader.h:34
gdcm::Reader
Reader ala DOM (Document Object Model).
Definition gdcmReader.h:54
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm::Writer
Writer ala DOM (Document Object Model).
Definition gdcmWriter.h:49