SimpleITK  1.2.4
Python/DicomModifyTags.py
1 #=========================================================================
2 #
3 # Copyright Insight Software Consortium
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0.txt
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 #=========================================================================
18 
19 from __future__ import print_function
20 
21 import SimpleITK as sitk
22 
23 import sys, time
24 
25 if len( sys.argv ) < 3:
26  print( "Usage: python " + __file__ + " <input_image> <output_image>" )
27  sys.exit ( 1 )
28 
29 # Read the image, single 3D DICOM slice
30 image = sitk.ReadImage( sys.argv[1] )
31 
32 # Modify the image (mean)
33 mean_image = sitk.BoxMean( image, [3,3,1] )
34 
35 # Save the modified image:
36 # We do not provide a method that copies all keys blindly. Our intent is
37 # to force us to remember that we always need to modify the meta-data dicitonary
38 # keys when we save processed images in DICOM format.
39 # In case of the DICOM format, amongst other keys we need to set the Image Type (0008,0008),
40 # the Series Date (0008,0021), and Series Time (0008,0021). Obviously we need to set a
41 # different series number (0020,0011), series instance UID (0020,000E), etc. - we don't do
42 # that here.
43 # Please see the DICOM standard (http://dicom.nema.org/standard.html) for complete details on
44 # how to create valid DICOM images.
45 
46 all_keys = image.GetMetaDataKeys()
47 for key in all_keys:
48  mean_image.SetMetaData( key, image.GetMetaData( key ) )
49 mean_image.SetMetaData( "0008|0008", "DERIVED\SECONDARY" )
50 modification_time = time.strftime("%H%M%S")
51 modification_date = time.strftime("%Y%m%d")
52 mean_image.SetMetaData( "0008|0031", modification_time )
53 mean_image.SetMetaData( "0008|0021", modification_date )
54 
55 sitk.WriteImage( mean_image, sys.argv[2] )
56 
57 # Finally, read the image back and see that changes were made
58 # Note that the image type (0008|0008) can contain additional spaces. The requirement is that
59 # the string have an even length (ftp://dicom.nema.org/medical/DICOM/2013/output/chtml/part05/sect_6.2.html).
60 # Where spaces are added is not specified and thus may vary ("DERIVED\SECONDARY ", "DERIVED\ SECONDARY" are
61 # equivalent).
62 modified_image = sitk.ReadImage( sys.argv[2] )
63 if modified_image.GetMetaData( "0008|0008" ).replace(" ","") != "DERIVED\SECONDARY" or \
64  modified_image.GetMetaData( "0008|0031" ) != modification_time or \
65  modified_image.GetMetaData( "0008|0021" ) != modification_date:
66  sys.exit(1)
67 
68 sys.exit( 0 )