Screen capture of chart

I want to screen capture a chart I’ve generated (using python) for inclusion in a report.

The following code works great for a 3D display…

view = cap.viewFromNode(slicer.mrmlScene.GetNodeByID(‘vtkMRMLViewNode1’))
cap.captureImageFromView(view, latexFolder + ‘\’ + fileName + ‘3dImage.png’)

but I get just the same result if a chart is being displayed in the display window. I have tried looking for a chart specific view node such as ‘vtkMRMLPlotViewNodePlotView1’ but without any success .

Even with the screen capture widget I can’t figure out how to capture the chart on it’s own - although I can get the chart, with all the other windows, if I use the ‘capture all views’ option. The save button on the interactive chart also doesn’t seem to be working for me.

I am using the nightly build from 11/11 on windows.

I’ve updated ScreenCapture module logic so that it can capture plot views. This script will work in Slicer rev28626 and later:

# Get a volume from SampleData and compute its histogram
import SampleData
import numpy as np
volumeNode = SampleData.SampleDataLogic().downloadMRHead()
histogram = np.histogram(arrayFromVolume(volumeNode), bins=50)
chartNode = slicer.util.plot(histogram, xColumnIndex = 1)
chartNode.SetYAxisRangeAuto(False)
chartNode.SetYAxisRange(0, 4e5)

# Capture plot view
viewNodeID = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLPlotViewNode").GetID()
import ScreenCapture
cap = ScreenCapture.ScreenCaptureLogic()
view = cap.viewFromNode(slicer.mrmlScene.GetNodeByID(viewNodeID))
cap.captureImageFromView(view,'c:/tmp/test.png')

That’s brilliant, thanks.

It’s now working exactly as I was expecting it to.

1 Like