# How to save slices of the image from nifti image?

**URL:** <https://discourse.itk.org/t/how-to-save-slices-of-the-image-from-nifti-image/1044>\
**Category:** Algorithms\
**Created:** [June 28, 2018, 6:41pm UTC](https://discourse.itk.org/t/how-to-save-slices-of-the-image-from-nifti-image/1044 "2018-06-28T18:41:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lee](https://discourse.itk.org/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://discourse.itk.org/u/lee)\
**Post date:** [June 28, 2018, 6:41pm UTC](https://discourse.itk.org/t/how-to-save-slices-of-the-image-from-nifti-image/1044/1 "2018-06-28T18:41:22Z")

</div>

I have read an image and converted into array. then I took a slice. but it seems to work.But when I apply RescaleIntensity or any other function I’m getting error

import SimpleITK as sitk  
img = sitk.ReadImage(“S1.nii”)  
a=sitk.GetArrayFromImage(img)  
im=a[:,:,19]

Error is:

TypeError Traceback (most recent call last)  
 in ()  
----\> 1 img\_255 = sitk.Cast(sitk.RescaleIntensity(im), sitk.sitkUInt8)

~/sitkpy/lib/python3.5/site-packages/SimpleITK/SimpleITK.py in RescaleIntensity(image1, outputMinimum, outputMaximum)  
58734  
58735 “”"

> 58736 return \_SimpleITK.RescaleIntensity(image1, outputMinimum, outputMaximum)  
> 58737 class RichardsonLucyDeconvolutionImageFilter(ImageFilter\_2):  
> 58738 “”"

TypeError: in method ‘RescaleIntensity’, argument 1 of type ‘itk::simple::Image const &’  
img\_255 = sitk.Cast(sitk.RescaleIntensity(im), sitk.sitkUInt8)

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [June 28, 2018, 6:55pm UTC](https://discourse.itk.org/t/how-to-save-slices-of-the-image-from-nifti-image/1044/2 "2018-06-28T18:55:45Z")

</div>

There is no need to convert to a numpy array. SimpleITK filters and functions operate on sitkImage’s and NOT numpy arrays. That is the case of your expecption.

The SimpleITK Image support pythonic slicing, so you don’t have a need to convert to numpy here. See this notebook:  
[http://insightsoftwareconsortium.github.io/SimpleITK-Notebooks/Python\_html/02\_Pythonic\_Image.html](http://insightsoftwareconsortium.github.io/SimpleITK-Notebooks/Python_html/02_Pythonic_Image.html)

---

<div class="post-metadata">

**Author:** ![lee](https://discourse.itk.org/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://discourse.itk.org/u/lee)\
**Post date:** [June 29, 2018, 1:08pm UTC](https://discourse.itk.org/t/how-to-save-slices-of-the-image-from-nifti-image/1044/3 "2018-06-29T13:08:42Z")

</div>

thanks
