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 78 79 80 81
|
############################################################################
#
# 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.
#
############################################################################
import gdcm
import os,sys
def TestAnonymizer(filename, verbose = False):
r = gdcm.Reader()
r.SetFileName( filename )
success = r.Read()
if( not success ): return 1
#print r.GetFile().GetDataSet()
ano = gdcm.Anonymizer()
ano.SetFile( r.GetFile() )
# 1. Replace with another value
ano.Replace( gdcm.Tag(0x0010,0x0010), "Test^Anonymize" )
# 2. Remove a tag (even a SQ)
ano.Remove( gdcm.Tag(0x0008,0x2112) )
# 3. Make a tag empty
ano.Empty( gdcm.Tag(0x0008,0x0070) )
# Call the main function:
success = ano.RemovePrivateTags() # do it !
if( not success ): return 1
# Check we can also change value from binary field
#ano.Replace( gdcm.Tag(0x0010,0x0010), "16", gdcm. )
# Let's check if our anonymization worked:
if verbose:
print(ano.GetFile().GetDataSet())
# So at that point r.GetFile() was modified, let's simply passed it to the Writer:
# First find a place where to write it out:
subdir = "TestAnonymizerPython"
tmpdir = gdcm.Testing.GetTempDirectory( subdir )
if not gdcm.System.FileIsDirectory( tmpdir ):
gdcm.System.MakeDirectory( tmpdir )
# Ok directory does exist now, extract the name of the input file, and merge it in
# our newly created tmpdir:
outfilename = gdcm.Testing.GetTempFilename( filename, subdir )
w = gdcm.Writer()
w.SetFileName( outfilename )
w.SetFile( r.GetFile() )
w.SetCheckFileMetaInformation( False )
success = w.Write()
if( not success ): return 1
if verbose:
print("Success to write: %s"%outfilename)
return success
if __name__ == "__main__":
success = 0
try:
filename = os.sys.argv[1]
success += TestAnonymizer( filename, True )
except:
# loop over all files:
t = gdcm.Testing()
nfiles = t.GetNumberOfFileNames()
for i in range(0,nfiles):
filename = t.GetFileName(i)
success += TestAnonymizer( filename )
# Test succeed ?
sys.exit(success == 0)
|