# How to script into code data loading, 3D model generation and screen capture tasks

**URL:** <https://discourse.slicer.org/t/how-to-script-into-code-data-loading-3d-model-generation-and-screen-capture-tasks/30228>\
**Category:** Support\
**Created:** [June 26, 2023, 2:16am UTC](https://discourse.slicer.org/t/how-to-script-into-code-data-loading-3d-model-generation-and-screen-capture-tasks/30228 "2023-06-26T02:16:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhlegarreta](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jhlegarreta/32/66542_2.png) [@jhlegarreta](https://discourse.slicer.org/u/jhlegarreta)\
**Post date:** [June 26, 2023, 2:16am UTC](https://discourse.slicer.org/t/how-to-script-into-code-data-loading-3d-model-generation-and-screen-capture-tasks/30228/1 "2023-06-26T02:16:22Z")

</div>

Hi,  
I’d like to be able to automate some tasks in 3D Slicer by calling its Python API from the command line:

- Loading structural (e.g. an T1w MRI) and segmentation files for a given subject.
- Making the data displayed at a given orientation/view (e.g. axial) at a specified slice/location/position (index).
- Potentially creating a 3D model/contour out of a segmentation, and display it in an orthogonal view
- Removing the default annotation labels (without requiring to restart 3D Slicer).
- Capturing a screenshot of a view (e.g. axial) at an arbitrary size and resolution.
- Setting the background of the view (whether a 3D or e.g. axial) to white or transparent (or black in the case of the 3D view).

So far I have found a way to capture the 3D or the orthogonal views using Python:

```auto
view = slicer.app.layoutManager().threeDWidget(0).threeDView()
renderWindow = view.renderWindow()
renderWindow.SetAlphaBitPlanes(1)
wti = vtk.vtkWindowToImageFilter()
wti.SetInputBufferTypeToRGBA()
wti.SetInput(renderWindow)
wti.Update()
vtk_data = wti.GetOutput()

writer = vtk.vtkPNGWriter()

writer.SetWriteToMemory(False)

writer.SetInputData(vtk_data)

filename = "/my_path/slicer_screenshot.png"
writer.SetFileName(filename)
writer.Write()

```

However, the above

- Does not remove the annotation labels. According to [Script repository — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#hide-slice-view-annotations), I need to execute the below to remove the labels, and this is only effective when restarting Slicer, which is inconvenient:

```auto
# Disable slice annotations immediately
sliceAnnotations = slicer.modules.DataProbeInstance.infoWidget.sliceAnnotations
sliceAnnotations.sliceViewAnnotationsEnabled=False
sliceAnnotations.updateSliceViewFromGUI()
# Disable slice annotations persistently (after Slicer restarts)
settings = qt.QSettings()
settings.setValue("DataProbe/sliceViewAnnotations.enabled", 0)

```

- The size of the captured screenshot has the size of the view/window (i.e it is a small window if I am in a 3D+orthogonal layout, a bigger one if I am on a single view layout, etc.). Can this be made arbitrarily big programmatically? If not, can the layout be maximized to a given view programmatically?

Can pointers or snippets be provided for the rest of the tasks in the bullet list as well, please?

Thanks for developing and maintaining this tool
