1
14
15"""
16
17 This small code shows how to use the gdcm.ImageRegionReader API
18 In this example we are taking each frame by frame and dump them to
19 /tmp/frame.raw.
20
21 Usage:
22 $ ExtractImageRegion.py input.dcm
23
24 Example:
25 $ ExtractImageRegion.py gdcmData/012345.002.050.dcm
26 $ md5sum /tmp/frame.raw
27 d594a5e2fde12f32b6633ca859b4d4a6 /tmp/frame.raw
28 $ gdcminfo --md5sum gdcmData/012345.002.050.dcm
29 [...]
30 md5sum: d594a5e2fde12f32b6633ca859b4d4a6
31"""
32
33import gdcm
34
35if __name__ == "__main__":
36 import sys
37 filename = sys.argv[1]
38
40
41
43 reader.SetFileName( filename );
44
45
46 if not reader.ReadInformation():
47 sys.exit(1)
48
49
50 cur_pos = reader.GetStreamCurrentPosition();
51
52 remaining = file_size - cur_pos;
53
54 print("Remaining bytes to read (Pixel Data): %d" % remaining );
55
56
57 f = reader.GetFile();
58
59
61 print(dims)
62 pf = gdcm.ImageHelper.GetPixelFormatValue (f);
63 pixelsize = pf.GetPixelSize();
65 print( pi );
66
67
68 buffer = bytearray( dims[0] * dims[1] * pixelsize )
69
70
72 for z in range(0, dims[2]):
73
74
75 box.SetDomain(0, dims[0] - 1, 0, dims[1] - 1, z, z);
76
77 reader.SetRegion( box );
78
79
80
81
82
83
84 if reader.ReadIntoBuffer(buffer):
85 open('/tmp/frame.raw', 'wb').write(buffer)
86 else:
87
88 sys.exit(1)
Class for manipulation box region.
Definition gdcmBoxRegion.h:31
static PhotometricInterpretation GetPhotometricInterpretationValue(File const &f)
static std::vector< unsigned int > GetDimensionsValue(const File &f)
ImageRegionReader.
Definition gdcmImageRegionReader.h:35
static size_t FileSize(const char *filename)