Created
          April 23, 2024 17:49 
        
      - 
      
- 
        Save sarthakpati/e1395a5549c87bd152e2cbcf379d8c94 to your computer and use it in GitHub Desktop. 
    Cast an image to different pixel type using SimpleITK
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import SimpleITK as sitk | |
| image_to_cast = sitk.ReadImage("/path/to/image.nii.gz") | |
| options_for_casting = { | |
| "int64": sitk.sitkInt64, | |
| "int32": sitk.sitkInt32, | |
| "int16": sitk.sitkInt16, | |
| "int8": sitk.sitkInt8, | |
| "uint64": sitk.sitkUInt64, | |
| "uint32": sitk.sitkUInt32, | |
| "uint16": sitk.sitkUInt16, | |
| "uint8": sitk.sitkUInt8, | |
| } | |
| default_casting = "int16" | |
| cast_to_type = default_casting ## change this to the type you want to cast to | |
| image_casted = sitk.Cast(image_to_cast, options_for_casting[cast_to_type]) | |
| sitk.WriteImage(image_casted, f"/path/to/image_{cast_to_type}.nii.gz") | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment