GDCM: FixCommaBug.py

GDCM 3.2.11

FixCommaBug.py
"""
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
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