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 """ An example script showing how to modify the tags of a DICOM image with SimpleITK. """
21 
22 import sys
23 import time
24 import SimpleITK as sitk
25 
26 if len(sys.argv) < 3:
27  print("Usage: python " + __file__ + " <input_image> <output_image>")
28  sys.exit(1)
29 
30 # Read the image, single 3D DICOM slice
31 image = sitk.ReadImage(sys.argv[1])
32 
33 # Modify the image (mean)
34 mean_image = sitk.BoxMean(image, [3, 3, 1])
35 
36 # Save the modified image:
37 # We do not provide a method that copies all keys blindly. Our intent is
38 # to force us to remember that we always need to modify the meta-data
39 # dictionary keys when we save processed images in DICOM format.
40 # In case of the DICOM format, amongst other keys we need to set the Image Type
41 # (0008,0008), the Series Date (0008,0021), and Series Time (0008,0021).
42 # Obviously we need to set a different series number (0020,0011), series
43 # instance UID (0020,000E), etc. - we don't do that here.
44 # Please see the DICOM standard (http://dicom.nema.org/standard.html) for
45 # complete details on how to create valid DICOM images.
46 
47 all_keys = image.GetMetaDataKeys()
48 for key in all_keys:
49  mean_image.SetMetaData(key, image.GetMetaData(key))
50 mean_image.SetMetaData("0008|0008", "DERIVED\\SECONDARY")
51 modification_time = time.strftime("%H%M%S")
52 modification_date = time.strftime("%Y%m%d")
53 mean_image.SetMetaData("0008|0031", modification_time)
54 mean_image.SetMetaData("0008|0021", modification_date)
55 
56 sitk.WriteImage(mean_image, sys.argv[2])
57 
58 # Finally, read the image back and see that changes were made.
59 # Note that the image type (0008|0008) can contain additional spaces. The
60 # requirement is that the string have an even length
61 # (ftp://dicom.nema.org/medical/DICOM/2013/output/chtml/part05/sect_6.2.html).
62 # Where spaces are added is not specified and thus may vary
63 # ("DERIVED\SECONDARY ", "DERIVED\ SECONDARY" are equivalent).
64 modified_image = sitk.ReadImage(sys.argv[2])
65 if (
66  modified_image.GetMetaData("0008|0008").replace(" ", "") != "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::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.
itk::simple::WriteImage
SITKIO_EXPORT void WriteImage(const Image &image, const std::vector< PathType > &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< PathType > &fileNames, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageSeriesReader class which is convenient for most image...