GDCM  2.4.5
DumbAnonymizer.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 This example shows how one can use the gdcm.Anonymizer in 'dumb' mode.
17 This class becomes really handy when one knows which particular tag to fill in.
18 
19 Usage:
20 
21  python DumbAnonymizer.py gdcmData/012345.002.050.dcm out.dcm
22 
23 """
24 
25 import gdcm
26 
27 # http://www.oid-info.com/get/1.3.6.1.4.17434
28 THERALYS_ORG_ROOT = "1.3.6.1.4.17434"
29 
30 tag_rules={
31  # Value
32  (0x0012,0x0010):("Value","MySponsorName"),
33  (0x0012,0x0020):("Value","MyProtocolID"),
34  (0x0012,0x0021):("Value","MyProtocolName"),
35  (0x0012,0x0062):("Value","YES"),
36  (0x0012,0x0063):("Value","MyDeidentificationMethod"),
37 
38  # Method
39  #(0x0002,0x0003):("Method","GenerateMSOPId"),
40  #(0x0008,0x1155):("Method","GenerateMSOPId"),
41  (0x0008,0x0018):("Method","GenerateMSOPId"),
42  (0x0010,0x0010):("Method","GetSponsorInitials"),
43  (0x0010,0x0020):("Method","GetSponsorId"),
44  (0x0012,0x0030):("Method","GetSiteId"),
45  (0x0012,0x0031):("Method","GetSiteName"),
46  (0x0012,0x0040):("Method","GetSponsorId"),
47  (0x0012,0x0050):("Method","GetTPId"),
48  (0x0018,0x0022):("Method","KeepIfExist"),
49  (0x0018,0x1315):("Method","KeepIfExist"),
50  (0x0020,0x000d):("Method","GenerateStudyId"),
51  (0x0020,0x000e):("Method","GenerateSeriesId"),
52  (0x0020,0x1002):("Method","GetNumberOfFrames"),
53  (0x0020,0x0020):("Method","GetPatientOrientation"),
54  # Other:
55  (0x0012,0x0051):("Patient Field","Type Examen"),
56  (0x0018,0x1250):("Sequence Field","Receive Coil"),
57  (0x0018,0x0088):("Sequence Field","Spacing Between Slice"),
58  (0x0018,0x0095):("Sequence Field","Pixel Bandwidth"),
59  (0x0018,0x0082):("Sequence Field","Invertion Time"),
60 }
61 
62 class MyAnon:
63  def __init__(self):
64  self.studyuid = None
65  self.seriesuid = None
66  generator = gdcm.UIDGenerator()
67  if not self.studyuid:
68  self.studyuid = generator.Generate()
69  if not self.seriesuid:
70  self.seriesuid = generator.Generate()
71  def GetSponsorInitials(self):
72  return "dummy^foobar"
73  def GenerateStudyId(self):
74  return self.studyuid
75  def GenerateSeriesId(self):
76  return self.seriesuid
77  #def GenerateMSOPId(self):
78  def GenerateMSOPId(self):
79  generator = gdcm.UIDGenerator()
80  return generator.Generate()
81  def GetSiteId(self):
82  return "MySiteId"
83  def GetSiteName(self):
84  return "MySiteName"
85  def GetSponsorId(self):
86  return "MySponsorId"
87  def GetTPId(self):
88  return "MyTP"
89 
90 if __name__ == "__main__":
91  import sys
93  gdcm.UIDGenerator.SetRoot( THERALYS_ORG_ROOT )
94 
95  r = gdcm.Reader()
96  filename = sys.argv[1]
97  r.SetFileName( filename )
98  if not r.Read(): sys.exit(1)
99 
100  obj = MyAnon()
101 
102  w = gdcm.Writer()
103  ano = gdcm.Anonymizer()
104  ano.SetFile( r.GetFile() )
105  ano.RemoveGroupLength()
106  for tag,rule in tag_rules.items():
107  if rule[0] == 'Value':
108  print tag,rule
109  ano.Replace( gdcm.Tag( tag[0], tag[1] ), rule[1] )
110  elif rule[0] == 'Method':
111  print tag,rule
112  # result = locals()[rule[1]]()
113  methodname = rule[1]
114  if hasattr(obj, methodname):
115  _member = getattr(obj, methodname)
116  result = _member()
117  ano.Replace( gdcm.Tag( tag[0], tag[1] ), result )
118  else:
119  print "Problem with: ", methodname
120 
121  outfilename = sys.argv[2]
122  w.SetFileName( outfilename )
123  w.SetFile( ano.GetFile() )
124  if not w.Write(): sys.exit(1)

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