SimpleITK  
Python/DicomModifyTags.py
1 # =========================================================================
2 #
3 # Copyright NumFOCUS
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 
20 import SimpleITK as sitk
21 
22 import sys
23 import 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
38 # dictionary 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
40 # (0008,0008), the Series Date (0008,0021), and Series Time (0008,0021).
41 # Obviously we need to set a different series number (0020,0011), series
42 # instance UID (0020,000E), etc. - we don't do that here.
43 # Please see the DICOM standard (http://dicom.nema.org/standard.html) for
44 # complete details on 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
59 # requirement is that the string have an even length
60 # (ftp://dicom.nema.org/medical/DICOM/2013/output/chtml/part05/sect_6.2.html).
61 # Where spaces are added is not specified and thus may vary
62 # ("DERIVED\SECONDARY ", "DERIVED\ SECONDARY" are equivalent).
63 modified_image = sitk.ReadImage(sys.argv[2])
64 if (
65  modified_image.GetMetaData("0008|0008").replace(" ", "")
66  != "DERIVED\\SECONDARY"
67  or modified_image.GetMetaData("0008|0031") != modification_time
68  or modified_image.GetMetaData("0008|0021") != modification_date
69 ):
70  sys.exit(1)
71 
72 sys.exit(0)
itk::simple::WriteImage
SITKIO_EXPORT void WriteImage(const Image &image, const std::vector< std::string > &fileNames, bool useCompression=false, int compressionLevel=-1)
WriteImage is a procedural interface to the ImageSeriesWriter. class which is convenient for many ima...
itk::simple::ReadImage
SITKIO_EXPORT Image ReadImage(const std::vector< std::string > &fileNames, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageSeriesReader class which is convenient for most image...
itk::simple::BoxMean
Image BoxMean(const Image &image1, std::vector< unsigned int > radius=std::vector< unsigned int >(3, 1))
Implements a fast rectangular mean filter using the accumulator approach.