# Is there any way yo directly extract an image from vtkImageData to export it as a tiff/png image?

**URL:** <https://discourse.slicer.org/t/is-there-any-way-yo-directly-extract-an-image-from-vtkimagedata-to-export-it-as-a-tiff-png-image/23704>\
**Category:** Development\
**Tags:** volume, file-export\
**Created:** [June 4, 2022, 9:53pm UTC](https://discourse.slicer.org/t/is-there-any-way-yo-directly-extract-an-image-from-vtkimagedata-to-export-it-as-a-tiff-png-image/23704 "2022-06-04T21:53:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chz31](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chz31/32/77363_2.png) [@chz31](https://discourse.slicer.org/u/chz31)\
**Post date:** [June 4, 2022, 9:53pm UTC](https://discourse.slicer.org/t/is-there-any-way-yo-directly-extract-an-image-from-vtkimagedata-to-export-it-as-a-tiff-png-image/23704/1 "2022-06-04T21:53:36Z")

</div>

Hi,

I am testing loading a series of rgb images as a volume using the ImageStacksModule, cropped them with an ROI, and export them as a new series of rgb images in the format of tiff or png.

An example image is here:

 ![DSC_1705](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/4/f44af3d945dd9e6dcd1c274ba1f712be07fe4467.jpeg)

So far, I can successfully export a slice of image from a volume by first extracting the numpy array of an image from a vector volume. I then use this numpy array to create a new vector volume node `vectorVolume` that only contains one image.

After that, I can use `vectorVolume.GetImageData()` to extract the `vtkImageData` of the `vectorVolume`, which now contains only 1 image. This allows me export it using `vtkTIFFWriter()` or `vtkPNGWriter()`, which can only take the `vtkImageData` object.

However, for every image in the original volume, I have to create a new volume with just one image to export it.

Therefore, I’m also thinking about directly output each image from the vtkImageData of the volume acquired from vectorVolumeNode.GetImageData() so that I don’t have to build a new volume for each image.

I could not find a way to do it. For an experiment, I tried to directly pass the numpy array of an image into vtkImageData object following the example here: [How to convert 3D numpy array to vtk and save the .vtk file? - #3 by user4](https://discourse.slicer.org/t/how-to-convert-3d-numpy-array-to-vtk-and-save-the-vtk-file/22327/3)

```auto
image = imageArray[0, :, :, :] #Get the first image array from slicer.util.arrayFromVolume(vectorVolume)
shape = image.shape
vtype = vtk.util.numpy_support.get_vtk_array_type(image.dtype)

flat_img_array = image.flatten()
vtk_arr = vtk.util.numpy_support.numpy_to_vtk(num_array=flat_img_array, deep=True, array_type=vtype)

imgVTK = vtk.vtkImageData()
imgVTK.GetPointData().SetScalars(vtk_arr)
imgVTK.SetDimensions(shape)

```

I then exported the imgVTK as PNG:

```auto
w_png = vtk.vtkPNGWriter()
w_png.SetInputData(imgVTK)
w_png.GetInput()
w_png.SetFileName('tmp/test.png')
w_png.Write()

```

However, all I got is an image with pixels re-arranged.

 ![test3](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/9/093e18fb47e60bee2a721f02164d3b7f9188c241.png)

Is there any way I can directly extract each image from `vectorVolumeNode.GetImageData()` as a single `vtkImageData` object and export it using `vtkTIFFWriter` without building a new volume for each image?

Thank you!

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [June 5, 2022, 11:04am UTC](https://discourse.slicer.org/t/is-there-any-way-yo-directly-extract-an-image-from-vtkimagedata-to-export-it-as-a-tiff-png-image/23704/2 "2022-06-05T11:04:03Z")

</div>

I don’t know if it helpful but have you tried using vtkOriemtedImageData?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [June 5, 2022, 6:22pm UTC](https://discourse.slicer.org/t/is-there-any-way-yo-directly-extract-an-image-from-vtkimagedata-to-export-it-as-a-tiff-png-image/23704/3 "2022-06-05T18:22:03Z")

</div>

Looks like the data is there, just scrambled. The image you get out is going to be 2D rgb but in your code you set the `vtkImageData` dimensions as if it were 3D. You need to set the width and height dimensions and the slice dimension to be 1. Then you set the number of components to be 3, Probably best using `AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 3)`.

> [@chz31](#):
>
> Is there any way I can directly extract each image from `vectorVolumeNode.GetImageData()` as a single `vtkImageData` object and export it using `vtkTIFFWriter` without building a new volume for each image?

If you want one tiff file per frame, then you can just use the same `vtkImageData` in a loop that just copies the data and re-runs the writer. It should run pretty fast.

---

<div class="post-metadata">

**Author:** ![chz31](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chz31/32/77363_2.png) [@chz31](https://discourse.slicer.org/u/chz31)\
**Post date:** [June 5, 2022, 9:46pm UTC](https://discourse.slicer.org/t/is-there-any-way-yo-directly-extract-an-image-from-vtkimagedata-to-export-it-as-a-tiff-png-image/23704/4 "2022-06-05T21:46:51Z")

</div>

Thanks! I’ll try that, but I could not find information about this method. Can you send me an introduction of vtkOrientedImageData?

---

<div class="post-metadata">

**Author:** ![chz31](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chz31/32/77363_2.png) [@chz31](https://discourse.slicer.org/u/chz31)\
**Post date:** [June 5, 2022, 9:58pm UTC](https://discourse.slicer.org/t/is-there-any-way-yo-directly-extract-an-image-from-vtkimagedata-to-export-it-as-a-tiff-png-image/23704/5 "2022-06-05T21:58:14Z")

</div>

Thank you, Steve! What I did successfully is to use `slicer.util.arrayFromVolume` to get the np array for all rgb images, and then create a single-image volume for each image using `slicer.util.updateVolumeFromArray(outputNode, singleImageArray)`. From each single-image volume, I then get the vtkImage data and export it as tiff/png.

This is still relatively fast, but being able to loop through the original multi-image `vtkImageData` would be most straightforward and efficient.

However, I could not find a way to directly loop through a multi-image `vtkImageData`. Can you give me some suggestions about how to do it, or maybe what I did about building a volume for each image is proper?

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [June 6, 2022, 7:58am UTC](https://discourse.slicer.org/t/is-there-any-way-yo-directly-extract-an-image-from-vtkimagedata-to-export-it-as-a-tiff-png-image/23704/6 "2022-06-06T07:58:43Z")

</div>

> [@mau\_igna\_06](#):
>
> vtkOriemtedImageData

Here you have code with explamations

> <https://github.com/Slicer/Slicer/blob/0094e9a35bd266cc8b0e677858dabce797c9928f/Libs/vtkSegmentationCore/vtkOrientedImageData.cxx>

Mauro

---

<div class="post-metadata">

**Author:** ![chz31](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chz31/32/77363_2.png) [@chz31](https://discourse.slicer.org/u/chz31)\
**Post date:** [June 6, 2022, 6:23pm UTC](https://discourse.slicer.org/t/is-there-any-way-yo-directly-extract-an-image-from-vtkimagedata-to-export-it-as-a-tiff-png-image/23704/7 "2022-06-06T18:23:27Z")

</div>

I figured what I did wrong. In addition to the dimensions and `AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 3)` as you suggested, I also forgot to set up the component number for the vtk array. of the rgb image using `SetNumberOfComponents`. I did:

```auto
channel_count =3
vtk_arr.SetNumberOfComponents(channel_count) #vtk_arr is the vtkDataArray from the flattened np image array of the single RGB image

```

Then I successfully output a slice of cropped image! Thanks!

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [June 6, 2022, 6:40pm UTC](https://discourse.slicer.org/t/is-there-any-way-yo-directly-extract-an-image-from-vtkimagedata-to-export-it-as-a-tiff-png-image/23704/8 "2022-06-06T18:40:14Z")

</div>

I think that’s veey fantastix
