SimpleITK  1.0.1
DicomSeriesReadModifyWrite/DicomSeriesReadModifySeriesWrite.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, os
24 
25 if len( sys.argv ) < 3:
26  print( "Usage: python " + __file__ + " <input_directory_with_DICOM_series> <output_directory>" )
27  sys.exit ( 1 )
28 
29 
30 # Read the original series. First obtain the series file names using the
31 # image series reader.
32 data_directory = sys.argv[1]
33 series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(data_directory)
34 if not series_IDs:
35  print("ERROR: given directory \""+data_directory+"\" does not contain a DICOM series.")
36  sys.exit(1)
37 series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(data_directory, series_IDs[0])
38 
39 # Second, to construct a proper volume from the series we must read
40 # the series with the image series reader.
41 series_reader = sitk.ImageSeriesReader()
42 series_reader.SetFileNames(series_file_names)
43 image3D = series_reader.Execute()
44 
45 # Lastly, to load the image's DICOM meta-data dictionaries, we read
46 # each image using an image reader that is set to load all DICOM tags
47 # (public+private). The resulting images contain their DICOM
48 # meta-data dictionaries.
49 image_reader = sitk.ImageFileReader()
50 image_reader.LoadPrivateTagsOn()
51 image_list = []
52 for file_name in series_file_names:
53  image_reader.SetFileName(file_name)
54  img = image_reader.Execute()
55  image_list.append(img)
56 
57 
58 
59 # Modify the image (blurring)
60 filtered_image = sitk.DiscreteGaussian(image3D)
61 
62 # Write the 3D image as a series
63 # IMPORTANT: There are many DICOM tags that need to be updated when you modify an
64 # original image. This is a delicate opration and requires knowlege of
65 # the DICOM standard. This example only modifies some. For a more complete
66 # list of tags that need to be modified see:
67 # http://gdcm.sourceforge.net/wiki/index.php/Writing_DICOM
68 
69 writer = sitk.ImageFileWriter()
70 # Use the study/seriers/frame of reference information given in the meta-data
71 # dictionary and not the automatically generated information from the file IO
72 writer.KeepOriginalImageUIDOn()
73 modification_time = time.strftime("%H%M%S")
74 modification_date = time.strftime("%Y%m%d")
75 for i in range(filtered_image.GetDepth()):
76  image_slice = filtered_image[:,:,i]
77  original_slice = image_list[i]
78  # Copy the meta-data except the rescale-intercept, rescale-slope
79  for k in original_slice.GetMetaDataKeys():
80  if k!="0028|1052" and k!= "0028|1053":
81  image_slice.SetMetaData(k, original_slice.GetMetaData(k))
82  # Set relevant keys indicating the change, modify or remove private tags as needed
83  image_slice.SetMetaData("0008|0031", modification_time)
84  image_slice.SetMetaData("0008|0021", modification_date)
85  image_slice.SetMetaData("0008|0008", "DERIVED\SECONDARY")
86  # Each of the UID components is a number (cannot start with zero) and separated by a '.'
87  # We create a unique series ID using the date and time.
88  image_slice.SetMetaData("0020|000e", "1.2.826.0.1.3680043.2.1125."+modification_date+".1"+modification_time)
89  # Write to the output directory and add the extension dcm if not there, to force writing is in DICOM format.
90  writer.SetFileName(os.path.join(sys.argv[2], os.path.basename(series_file_names[i])) + ('' if os.path.splitext(series_file_names[i])[1] == '.dcm' else '.dcm'))
91  writer.Execute(image_slice)
92 sys.exit( 0 )