def anonymize(self): """ According to PS 3.15-2008, basic application level De-Indentification of a DICOM file requires replacing the values of a set of data elements""" self._anon_obj = gdcm.Anonymizer() self._anon_obj.SetFile(self._file) self._anon_obj.RemoveGroupLength() if self._anon_tags is None: self._anon_tags = get_anon_tags() for tag in self._anon_tags: cur_tag = tag['Tag'].replace("(", "") cur_tag = cur_tag.replace(")", "") name = tag["Attribute Name"].replace(" ", "").encode("utf8") group, element = cur_tag.split(",", 1) # TODO expand this 50xx, 60xx, gggg, eeee if ("xx" not in group and "gggg" not in group and "eeee" not in group): group = int(group, 16) element = int(element, 16) if self.find(group=group, element=element): self._anon_obj.Replace(gdcm.Tag(group, element), "Anon" + name) return self._anon_obj
def TestAnonymizer(filename, verbose=False): r = gdcm.Reader() r.SetFileName(filename) sucess = r.Read() if (not sucess): 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: sucess = ano.RemovePrivateTags() # do it ! if (not sucess): 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) sucess = w.Write() if (not sucess): return 1 if verbose: print("Success to write: %s" % outfilename) return sucess
def __init__(self, key = None, cert = None, deterministicUIDs = True, \ generateDummyNames = True, salt = None, \ removeTags = None, addTags = None): self.anonymizer = gdcm.Anonymizer() self.dummy = True # if key and cert is specified, encryption mode is enabled if key is not None and cert is not None: # or OpenSSL or whatever self._cf = gdcm.CryptoFactory_GetFactoryInstance( gdcm.CryptoFactory.DEFAULT) self._cp = self._cf.CreateCMSProvider() if not self._cp.ParseKeyFile(key): raise ValueError(f'Key file {key} could not be read') if not self._cp.ParseCertificateFile(cert): raise ValueError(f'Certificate file {cert} could not be read') # this one could be changes if necessary self._cp.SetCipherType( gdcm.CryptographicMessageSyntax.AES256_CIPHER) self.anonymizer.SetCryptographicMessageSyntax(self._cp) self.dummy = False elif key is None or cert is None: raise ValueError( 'Both key and cert must be specified to disable dummy mode') if self.dummy: # for the getters self.deterministicUIDs = None self.generateDummyNames = None self.salt = '' else: # extension of Radpoint GDCM self.setDeterminicticUIDs(deterministicUIDs) self.setGenerateDummyNames(generateDummyNames) self.setSalt(salt) self.removeTagsFromBALCPA(removeTags) self.addTagsToBALCPA(addTags)
if __name__ == "__main__": import sys gdcm.FileMetaInformation.SetSourceApplicationEntityTitle("DumbAnonymizer") gdcm.UIDGenerator.SetRoot(THERALYS_ORG_ROOT) r = gdcm.Reader() filename = sys.argv[1] r.SetFileName(filename) if not r.Read(): sys.exit(1) obj = MyAnon() w = gdcm.Writer() ano = gdcm.Anonymizer() ano.SetFile(r.GetFile()) ano.RemoveGroupLength() for tag, rule in tag_rules.items(): if rule[0] == 'Value': print tag, rule ano.Replace(gdcm.Tag(tag[0], tag[1]), rule[1]) elif rule[0] == 'Method': print tag, rule # result = locals()[rule[1]]() methodname = rule[1] if hasattr(obj, methodname): _member = getattr(obj, methodname) result = _member() ano.Replace(gdcm.Tag(tag[0], tag[1]), result) else:
def remove_retired(f): an = gdcm.Anonymizer() an.SetFile(f) an.RemoveGroupLength() an.RemoveRetired() an.RemovePrivateTags()