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
22import sys
23import time
24import SimpleITK as sitk
25
26if 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
31image = sitk.ReadImage(sys.argv[1])
32
33# Modify the image (mean)
34mean_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
47all_keys = image.GetMetaDataKeys()
48for key in all_keys:
49 mean_image.SetMetaData(key, image.GetMetaData(key))
50mean_image.SetMetaData("0008|0008", "DERIVED\\SECONDARY")
51modification_time = time.strftime("%H%M%S")
52modification_date = time.strftime("%Y%m%d")
53mean_image.SetMetaData("0008|0031", modification_time)
54mean_image.SetMetaData("0008|0021", modification_date)
55
56sitk.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).
64modified_image = sitk.ReadImage(sys.argv[2])
65if (
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
72sys.exit(0)
SITKIO_EXPORT Image ReadImage(const PathType &filename, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageFileReader class which is convenient for most image r...
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.
SITKIO_EXPORT void WriteImage(const Image &image, const PathType &fileName, bool useCompression=false, int compressionLevel=-1)
WriteImage is a procedural interface to the ImageFileWriter. class which is convenient for many image...