# Add empty slice at the end of the CT image

**URL:** https://discourse.itk.org/t/add-empty-slice-at-the-end-of-the-ct-image/6182
**Category:** Beginner Questions
**Tags:** itk, python, nifti, simpleitk
**Created:** [September 6, 2023, 3:07pm UTC](https://discourse.itk.org/t/add-empty-slice-at-the-end-of-the-ct-image/6182 "2023-09-06T15:07:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![agatadefr](https://discourse.itk.org/user_avatar/discourse.itk.org/agatadefr/32/1629_2.png) [@agatadefr](https://discourse.itk.org/u/agatadefr)
#### Post date: [September 6, 2023, 3:07pm UTC](https://discourse.itk.org/t/add-empty-slice-at-the-end-of-the-ct-image/6182/1 "2023-09-06T15:07:04Z")

</div>

Hello,

I have a simple question. i need to resize CT scan saved in nifty file. I need to add one black slice at the end of the 3D image (in Z direction).  
I would appreciate for any help/suggestion. How can I do that in simpleitk ?  
Is there any simple way to modify that image ?

Best,  
Agata

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [September 6, 2023, 4:17pm UTC](https://discourse.itk.org/t/add-empty-slice-at-the-end-of-the-ct-image/6182/2 "2023-09-06T16:17:47Z")

</div>

Hello @agatadefr,

“one black slice” - this is not well defined as black is a display color, not an intensity value. As you are working with CT, likely you want to add a slice with air, which has a HU value of -1000 (often people equate “black” with 0 intensity).

Images in ITK/SimpleITK are spatial objects, so padding an image involves modifications to the spatial information.

Code below does what you want (significantly simplified based on the feedback below from @blowekamp). It can be further simplified to a one liner if you know which side you want to pad, wasn’t clear to me so this is a flag:

```auto
import SimpleITK as sitk

file_name = 
image = sitk.ReadImage(file_name)

air_hu = -1000
# which end of the slice stack to pad front or back
front_end = True

if front_end:
  padded_image = sitk.ConstantPad(image, [0,0,1], [0,0,0], air_hu)
else:
    padded_image = sitk.ConstantPad(image, [0,0,0],[0,0,1], air_hu)

```

---

<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: [September 6, 2023, 4:49pm UTC](https://discourse.itk.org/t/add-empty-slice-at-the-end-of-the-ct-image/6182/3 "2023-09-06T16:49:44Z")

</div>

It may be simpler to use the [ConstantPadImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1ConstantPadImageFilter.html).

---

<div class="post-metadata">

### Author: ![agatadefr](https://discourse.itk.org/user_avatar/discourse.itk.org/agatadefr/32/1629_2.png) [@agatadefr](https://discourse.itk.org/u/agatadefr)
#### Post date: [September 7, 2023, 12:30pm UTC](https://discourse.itk.org/t/add-empty-slice-at-the-end-of-the-ct-image/6182/4 "2023-09-07T12:30:17Z")

</div>

Thanks a lot @zivy ! It works so well. Thats exactly what I needed! I am so grateful for your help!
