# How to modify an array of a SITK object

**URL:** https://discourse.itk.org/t/how-to-modify-an-array-of-a-sitk-object/5872
**Category:** Beginner Questions
**Created:** [April 17, 2023, 3:03am UTC](https://discourse.itk.org/t/how-to-modify-an-array-of-a-sitk-object/5872 "2023-04-17T03:03:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![javidx](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@javidx](https://discourse.itk.org/u/javidx)
#### Post date: [April 17, 2023, 3:03am UTC](https://discourse.itk.org/t/how-to-modify-an-array-of-a-sitk-object/5872/1 "2023-04-17T03:03:08Z")

</div>

Hi, I’m a beginner in using SITK. I imagine this should be an easy thing to do but I couldn’t find anything that worked. Basically, I have a DICOM image that I’m loading with SITK. I also have a binary mask for this DICOM image. How can I easily apply this mask to this SITK image? I wanna keep the metadata on the original image intact

## Input DICOM Image

input\_img = sitk.ImageSeriesReader()  
dicom\_names = input\_img.GetGDCMSeriesFileNames(sys.argv[1])  
input\_img.SetFileNames(dicom\_names)

Mask = np.array(mask)

## Now, how can I apply Mask to the input\_img without modifying the metadata?

Thanks for your help!

---

<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: [April 17, 2023, 2:38pm UTC](https://discourse.itk.org/t/how-to-modify-an-array-of-a-sitk-object/5872/2 "2023-04-17T14:38:13Z")

</div>

Hello @javidx ,

See code below:

```auto
# Read first series from the given directory (sys.argv[1])
input_image = sitk.ReadImage(sitk.ImageSeriesReader_GetGDCMSeriesFileNames(sys.argv[1]))

# mask is a binary numpy array, so get the corresponding SimpleITK image
mask_image = sitk.GetImageFromArray(mask)
mask_image.CopyInformation(input_image)

# apply mask to input_img
foreground_image = input_image*sitk.Cast(mask_image, input_image.GetPixelID())

```

---

<div class="post-metadata">

### Author: ![javidx](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@javidx](https://discourse.itk.org/u/javidx)
#### Post date: [April 17, 2023, 8:38pm UTC](https://discourse.itk.org/t/how-to-modify-an-array-of-a-sitk-object/5872/3 "2023-04-17T20:38:02Z")

</div>

Thank you so much! Worked like a charm!
