GDCM 2.2.1

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

MergeFile.py
1 ############################################################################
2 #
3 # Program: GDCM (Grassroots DICOM). A DICOM library
4 #
5 # Copyright (c) 2006-2011 Mathieu Malaterre
6 # All rights reserved.
7 # See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8 #
9 # This software is distributed WITHOUT ANY WARRANTY; without even
10 # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 # PURPOSE. See the above copyright notice for more information.
12 #
13 ############################################################################
14
15 """
16 Usage:
17
18 python MergeFile.py input1.dcm input2.dcm
19
20 It will produce a 'merge.dcm' output file, which contains all meta information from input1.dcm
21 and copy the Stored Pixel values from input2.dcm
22 This script even works when input2.dcm is a Secondary Capture and does not contains information
23 such as IOP and IPP...
24 """
25
26 import sys
27 import gdcm
28
29 if __name__ == "__main__":
30
31 file1 = sys.argv[1]
32 file2 = sys.argv[2]
33
34 r1 = gdcm.ImageReader()
35 r1.SetFileName( file1 )
36 if not r1.Read():
37 sys.exit(1)
38
39 r2 = gdcm.ImageReader()
40 r2.SetFileName( file2 )
41 if not r2.Read():
42 sys.exit(1)
43
44 # Image from r2 could be Secondary Capture and thus would not contains neither IPP nor IOP
45 # Instead always prefer to only copy the Raw Data Element.
46 # Warning ! Image need to be identical ! Only the value of Stored Pixel can be different.
47 r1.GetImage().SetDataElement( r2.GetImage().GetDataElement() )
48
49 w = gdcm.ImageWriter()
50 w.SetFile( r1.GetFile() )
51 #w.SetImage( r2.GetImage() ) # See comment above
52 w.SetImage( r1.GetImage() )
53
54 w.SetFileName( "merge.dcm" )
55 if not w.Write():
56 sys.exit(1)
57
58 sys.exit(0)