ITK  5.4.0
Insight Toolkit
Examples/IO/DicomSliceRead.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# https://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# Example on the use of ImageFileReader to reading a single slice (it will read
21# DICOM or other format), rescale the intensities and save it in a different
22# file format.
23#
24
25import itk
26import sys
27
28if len(sys.argv) < 3:
29 print("Usage: " + sys.argv[0] + " inputFile.dcm outputFile.png")
30 sys.exit(1)
31
32#
33# Reads a 2D image in with signed short (16bits/pixel) pixel type
34# and save it as unsigned char (8bits/pixel) pixel type
35#
36InputImageType = itk.Image.SS2
37OutputImageType = itk.Image.UC2
38
39reader = itk.ImageFileReader[InputImageType].New()
40writer = itk.ImageFileWriter[OutputImageType].New()
41
42filter = itk.RescaleIntensityImageFilter[InputImageType, OutputImageType].New()
43filter.SetOutputMinimum(0)
44filter.SetOutputMaximum(255)
45
46filter.SetInput(reader.GetOutput())
47writer.SetInput(filter.GetOutput())
48
49reader.SetFileName(sys.argv[1])
50writer.SetFileName(sys.argv[2])
51
52writer.Update()
Data source that reads image data from a single file.
Writes image data to a single file.
Applies a linear transformation to the intensity levels of the input Image.
static Pointer New()