# Export segmentation axial slices and matching unsegmented axial slices as .png in different folders

**URL:** <https://discourse.slicer.org/t/export-segmentation-axial-slices-and-matching-unsegmented-axial-slices-as-png-in-different-folders/28915>\
**Category:** Development\
**Created:** [April 14, 2023, 12:39pm UTC](https://discourse.slicer.org/t/export-segmentation-axial-slices-and-matching-unsegmented-axial-slices-as-png-in-different-folders/28915 "2023-04-14T12:39:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ba\_Sar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ba_sar/32/65611_2.png) [@Ba\_Sar](https://discourse.slicer.org/u/Ba_Sar)\
**Post date:** [April 14, 2023, 12:39pm UTC](https://discourse.slicer.org/t/export-segmentation-axial-slices-and-matching-unsegmented-axial-slices-as-png-in-different-folders/28915/1 "2023-04-14T12:39:33Z")

</div>

![slicer_screenshot](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/7/f7017d3e961ef768a410f1f531af32a8abc56df4.jpeg)

Dear community,  
i hope you are doing well. I have a T2 TSE TRA MRI dicom of the spine. I used the axial plane to segment the dural sack in 3 different slices. now i want to accomplish the following task:  
1)export the segmented axial slices in the folder “segmented” as .png image  
2)export the unsegmented corresponding axial slices (same name as segmented slices) to the folder “unsegmented” as .png image

I was able to build the following Python code which was supposed to do this task, however, when accessing the folders they show only black images. Maybe you have a better automated solution for me. I thought such automated way would be faster then manually exporting the slices. I am confident that you as experts will have a better solution for me. I also attached the image of my subject hierarchy. Thank you very much in advance.

Code:

```python
import os
import vtk

# Set the export directories
non_segmented_export_directory = "C:\\Users\\Babak\\Desktop\\Automated_Dural_sack_measurement\\own_study\\non_segmented"
segmented_export_directory = "C:\\Users\\Babak\\Desktop\\Automated_Dural_sack_measurement\\own_study\\segmented"

# Get the original MRI volume node and the segmentation node
volume_node = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLScalarVolumeNode')
segmentation_node = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLSegmentationNode')

def capture_slice_screenshot(slice_widget, filename):
    # Capture the screenshot using vtkWindowToImageFilter
    window_to_image_filter = vtk.vtkWindowToImageFilter()
    window_to_image_filter.SetInput(slice_widget.sliceView().renderWindow())
    window_to_image_filter.Update()

    # Save the screenshot as a PNG file
    writer = vtk.vtkPNGWriter()
    writer.SetFileName(filename)
    writer.SetInputConnection(window_to_image_filter.GetOutputPort())
    writer.Write()

if volume_node is None or segmentation_node is None:
    print("Error: MRI volume or segmentation node not found. Please check the names and try again.")
else:
    # Create a labelmap from the segmentation
    labelmap_volume_node = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
    slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentation_node, labelmap_volume_node, volume_node)

    # Get the slice widget and slice node for the red slice view
    slice_widget = slicer.app.layoutManager().sliceWidget("Red")
    slice_logic = slice_widget.sliceLogic()
    slice_node = slice_logic.GetSliceNode()
    slice_composite_node = slice_logic.GetSliceCompositeNode()

    # Get the RAS to IJK matrix
    ras_to_ijk = vtk.vtkMatrix4x4()
    volume_node.GetRASToIJKMatrix(ras_to_ijk)

    # Loop through the axial slices
    for slice_index in range(volume_node.GetImageData().GetDimensions()[2]):

        # Get the RAS coordinates for the current slice
        ijk_coordinates = [0, 0, slice_index, 1]
        ras_coordinates = ras_to_ijk.MultiplyPoint(ijk_coordinates)

        # Set the current slice index
        slice_node.SetSliceOffset(ras_coordinates[2])

        # Save the non-segmented slice
        non_segmented_filename = os.path.join(non_segmented_export_directory, f"slice_{slice_index:03d}_non_segmented.png")
        slice_composite_node.SetBackgroundVolumeID(volume_node.GetID())
        slice_composite_node.SetLabelVolumeID(None)
        slice_widget.sliceView().forceRender()
        capture_slice_screenshot(slice_widget, non_segmented_filename)

        # Save the segmented slice
        segmented_filename = os.path.join(segmented_export_directory, f"slice_{slice_index:03d}_segmented.png")
        slice_composite_node.SetLabelVolumeID(labelmap_volume_node.GetID())
        slice_widget.sliceView().forceRender()
        capture_slice_screenshot(slice_widget, segmented_filename)

```

---

<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:** [April 14, 2023, 12:43pm UTC](https://discourse.slicer.org/t/export-segmentation-axial-slices-and-matching-unsegmented-axial-slices-as-png-in-different-folders/28915/2 "2023-04-14T12:43:18Z")

</div>

Maybe you can just use the [ScreenCapture](https://slicer.readthedocs.io/en/latest/user_guide/modules/screencapture.html) module. If you need to script the behavior you can look at how it’s done in the [Animator](https://github.com/SlicerMorph/SlicerMorph/blob/master/Animator/Animator.py) module.
