DICOM Anonymiser won't anonymise names?

I’m trying to write a DICOM anonymiser script. It is partially successful, in that it anonymises most of the data I ask it too. However, it is also completely useless as it also doesn’t anonymise either the patient of the physician’s name! This seems weird as it’s all contained in a for loop so what is done to one DICOM tag should be done to all the others. Is there anything I’m missing in this?

Here’s my code if that helps.

macro "DICOM Anon" {

//Creates array of relevant DICOM Tags and associated headers.
var Tags = newArray("0008,0020","0008,0021","0008,0022","0008,0023","0008,0050","0008,0080","0008,0081","0008,0090","0008,1048","0008,1050","0010,0010","0010,0020","0010,0021","0010,0030","0010,0032","0010,0040","0010,1010","0010,1030","0010,1040","0032,1032","0032,1033");
var Headers = newArray("  Study Date: ","  Series Date: ","  Acquisition Date: ","  Image Date: ","  Accession Number: ","  Institution Name: ","  Institution Address: ","  Referring Physician's Name: ","  Physician(s) of Record: ","  Attending Physician's Name: ","  Patient's Name: ","  Patient ID: ","  Issuer of Patient ID:  ","  Patient's Birth Date: ","  Patient's Birth Time: ","  Patient's Sex:  ","  Patient's Age: ","  Patient's Weight: ","  Patient's Address: ","  Requesting Physician:  ","  Requesting Service: ");
var labels = newArray(Headers.length);

//Remove colon from headers for neatness in checkbox

//Creates checkbox for specific anonymisation based on headers.
Dialog.create("Anonymiser Choices");

//Turns checkbox choices into an array so it can be used on multiple slices.

//Cylces through slices if checkbox it true, then searches for DICOM info, and replaces it with Anonymised
			//info = Tags[i]+Headers[i]+getInfo(Tags[i]);
			//anon =Tags[i]+Headers[i]+"";
			info = getInfo(Tags[i]);
			anon =" Anonymised";
		}//end if Dialog...
	}// end for i
}//end for n


If you want to install the free Orthanc, we already have an anonymizer for it.
http://petctviewer.org It may save you some effort.


Dear @2Shuze,

does your macro anonymize the other tags for your DICOM files?

I ran into the issue that when one of the tags was not set, getInfo(...) returns an empty string which seems to trigger undefined behavior of replace():

testString = "Test";
print(replace(testString, "", "X")); // XTXeXsXtX

It seems to replace each single character of testString with X<character>. Maybe that’s part of the issue?


1 Like

It’s not “undefined” actually: every match of the empty string gets replaced by the given replacement string. The Groovy, Python and Java replace functions work in the very same way:

print ("test".replace("", "X")) # XtXeXsXtX

In Python, you can also replace only a given number of occurrences:

print ("test".replace("", "X", 2)) # XtXest
1 Like

Sorry, I wasn’t clear about the definition I was referring to:

replace(string, old, new)
Returns the new string that results from replacing all occurrences of old in string with new, where old and new are single character strings. If old or new are longer than one character, each substring of string that matches the regular expression old is replaced with new.

From that description, I did not expect that behavior (also, I wasn’t aware of it in other languages).

Is that documented somewhere for the other languages?

The javadoc for String#replace states the following:

public String replace(CharSequence target, CharSequence replacement)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing “aa” with “b” in the string “aaa” will result in “ba” rather than “ab”.

But indeed, there seem to be some inconsistencies between lanugages:

BTW: the macro language function replace actually calls Java’s String#replaceAll() internally:

1 Like