# Save the vtkImageReslice 2D output to file

**URL:** https://discourse.vtk.org/t/save-the-vtkimagereslice-2d-output-to-file/8281
**Category:** Support
**Created:** [April 12, 2022, 10:45am UTC](https://discourse.vtk.org/t/save-the-vtkimagereslice-2d-output-to-file/8281 "2022-04-12T10:45:37Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jamsoft](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jamsoft](https://discourse.vtk.org/u/jamsoft)
#### Post date: [April 12, 2022, 10:45am UTC](https://discourse.vtk.org/t/save-the-vtkimagereslice-2d-output-to-file/8281/1 "2022-04-12T10:45:37Z")

</div>

Hello friends,

I have the following problem with saving the vtkImageReslice output to file using vtkBMPWriter.

The output of the vtkImageReslice in the vtkImageViewer2 looks good, as demonstrated in the following image:  
 ![axial](https://discourse.vtk.org/uploads/default/original/2X/3/39d7760b9e505c76036af2e0e1ef412203cf5ec6.jpeg)

It is exactly what I need.

Now, I need to save this output to the bmp file so I use vtkBMPWriter. The problem is that I need to set the vtkImageReslice output to the unsigned char as the vtkBMPWriter doesn’t support another input.

Then, when I save the output, it looks like on the following image:

![axial2](https://discourse.vtk.org/uploads/default/original/2X/6/6c07969080ce8f2d802537f97c77c5d209513633.jpeg)

There are some unwanted vertical lines.

Here is the code snippet:

```
       self.imageReslice = vtk.vtkImageReslice()
       self.imageReslice.SetInputConnection(self.extractor.GetOutputPort())
       self.imageReslice.SetOutputDimensionality(2)
       self.imageReslice.SetResliceAxesDirectionCosines(1, 0, 0, 0, 1, 0, 0, 0, 1) 
       self.imageReslice.InterpolateOn()
       self.imageReslice.SetOutputSpacing(xSpacing, ySpacing, zSpacing)
       self.imageReslice.SetResliceAxesOrigin(center1[0], center1[1], center1[2])
       self.imageReslice.AutoCropOutputOn()
       self.imageReslice.UpdateWholeExtent()
       
       viewer = vtk.vtkImageViewer2()
       viewer.SetInputConnection(self.imageReslice.GetOutputPort())
       viewer.SetColorWindow(255)
       viewer.SetColorLevel(127.5)
       viewer.Render()

       self.imageReslice.SetOutputScalarType(vtk.VTK_UNSIGNED_CHAR)

       self.writerAxial.SetInputConnection(self.imageReslice.GetOutputPort())
       self.writerAxial.SetFileName("axial.bmp")
       self.writerAxial.Update()
       self.writerAxial.Write()

```

What can I do to have result on the saved image the same as the result in the image viewer window?  
Do I need to add some filter or do anything else?

Thank you and have a nice day,

Jirka

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [April 12, 2022, 12:47pm UTC](https://discourse.vtk.org/t/save-the-vtkimagereslice-2d-output-to-file/8281/2 "2022-04-12T12:47:09Z")

</div>

You can convert the data type of `vtkImageReslice.GetOutput` from ‘float’ to `unsigned_char`.

The code would looks like:

```auto
imageReslice.Update()
imgCast = vtk.vtkImageCast()
imgCast.SetInputData(imageReslice.GetOutput())
imgCast.SetOutputScalrTypeToChar()
imgCast.Update()
...
writerAxial.SetInput(imgCast.GetOutput())

```

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [April 12, 2022, 12:51pm UTC](https://discourse.vtk.org/t/save-the-vtkimagereslice-2d-output-to-file/8281/3 "2022-04-12T12:51:51Z")

</div>

The BMP writer might require a 24-bit RGB image instead of just an 8-bit image.

Try using [vtkImageResliceToColors](https://vtk.org/doc/nightly/html/classvtkImageResliceToColors.html) instead of using vtkImageReslice with SetOutputScalarType().
