# Need help for a script of saving screenshot for many times

**URL:** <https://discourse.slicer.org/t/need-help-for-a-script-of-saving-screenshot-for-many-times/32877>\
**Category:** Support\
**Created:** [November 17, 2023, 3:57pm UTC](https://discourse.slicer.org/t/need-help-for-a-script-of-saving-screenshot-for-many-times/32877 "2023-11-17T15:57:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jp\_jp\_jp](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@jp\_jp\_jp](https://discourse.slicer.org/u/jp_jp_jp)\
**Post date:** [November 17, 2023, 3:57pm UTC](https://discourse.slicer.org/t/need-help-for-a-script-of-saving-screenshot-for-many-times/32877/1 "2023-11-17T15:57:41Z")

</div>

Hi, I am working on this red slice

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/9/f998821d88714c4f6f430911bd9c56ffb0887ab2.png)  
The range is from -500mm to 500mm, and all I know is I can save it by clicking Annotation Screenshot, and choosing Red Slice View  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/5/25d7e8c59764089530c926be8ef4ce8263e20fe7.png)  
It would be great if someone could provide a Python script to save the Red Slice every 10 mm as png in an absolute directory, for example, C:\Users\jp1234\Documents

Thank you!

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [November 17, 2023, 9:44pm UTC](https://discourse.slicer.org/t/need-help-for-a-script-of-saving-screenshot-for-many-times/32877/2 "2023-11-17T21:44:06Z")

</div>

[This snippet from the repository should get you started …](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#save-a-series-of-images-from-a-slice-view)

---

<div class="post-metadata">

**Author:** ![jp\_jp\_jp](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@jp\_jp\_jp](https://discourse.slicer.org/u/jp_jp_jp)\
**Post date:** [November 17, 2023, 10:21pm UTC](https://discourse.slicer.org/t/need-help-for-a-script-of-saving-screenshot-for-many-times/32877/3 "2023-11-17T22:21:03Z")

</div>

> [@jp\_jp\_jp](#):
>
> The range is from -500mm to 500mm, and all I know is I can save it by clicking Annotation Screenshot, and choosing Red Slice View  
> ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/5/25d7e8c59764089530c926be8ef4ce8263e20fe7.png)  
> It would be great if someone could provide a Python script to save the Red Slice every 10 mm as png in an absolute directory, for example, C:\Users\jp1234\Documents

Thanks, I created this one, and copied it to the Python Console, but nothing shows up

```auto
layoutName = "Red"
imagePathPattern = "C:/Users/jp1234/Documents/red_slice-%03d.png"
startOffset = -10
endOffset = 50
stepSize = 10

widget = slicer.app.layoutManager().sliceWidget(layoutName)
view = widget.sliceView()
logic = widget.sliceLogic()
bounds = [0,]*6
logic.GetSliceBounds(bounds)

currentOffset = startOffset
stepCount = 0

while currentOffset <= endOffset:
    logic.SetSliceOffset(currentOffset)
    view.forceRender()
    image = view.grab().toImage()
    image.save(imagePathPattern % stepCount)
    
    currentOffset += stepSize
    stepCount += 1

```

then I tried the example, but [Qt] QPixmap::grabWidget is deprecated, use QWidget::grab() instead

so I use this

```auto
img = qt.QWidget.grab(slicer.util.mainWindow()).toImage()
img.save("C:/Users/jp1234/Documents/red_slice-%03d.png")

```

Still nothing shows up

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [November 18, 2023, 9:35am UTC](https://discourse.slicer.org/t/need-help-for-a-script-of-saving-screenshot-for-many-times/32877/4 "2023-11-18T09:35:07Z")

</div>

> [@jp\_jp\_jp](#):
>
> Hi, I am working on this red slice
> 
> ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/9/f998821d88714c4f6f430911bd9c56ffb0887ab2.png)
> 
> The range is from -500mm to 500mm, and all I know is I can save it by clicking Annotation Screenshot, and choosing Red Slice View  
> ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/5/25d7e8c59764089530c926be8ef4ce8263e20fe7.png)  
> It would be great if someone could provide a Python script to save the Red Slice every 10 mm as png in an absolute directory, for example, C:\Users\jp1234\Documents
> 
> Thank you!

ChatGPT is here to help:-)

[https://chat.openai.com/share/b43f0fe7-44c3-49a5-afad-fffa27d1c5aa](https://chat.openai.com/share/b43f0fe7-44c3-49a5-afad-fffa27d1c5aa)

The last code iteration works:

```auto
import os
import slicer

# Define the directory to save images
output_directory = "C:/Users/yourname/Documents/SlicerOutput"
if not os.path.exists(output_directory):
    os.makedirs(output_directory)

# Define the range and step size
start = -500
end = 500
step = 10

# Get the Red Slice widget
red_logic = slicer.app.layoutManager().sliceWidget("Red").sliceLogic()

# Function to take and save screenshot
def saveScreenshot(sliceLogic, filename):
    layoutManager = slicer.app.layoutManager()
    sliceNode = sliceLogic.GetSliceNode()
    sliceView = layoutManager.sliceWidget(sliceNode.GetLayoutName()).sliceView()
    sliceView.forceRender()
    w2i = vtk.vtkWindowToImageFilter()
    w2i.SetInput(sliceView.renderWindow())
    w2i.Update()
    writer = vtk.vtkPNGWriter()
    writer.SetFileName(filename)
    writer.SetInputConnection(w2i.GetOutputPort())
    writer.Write()

# Iterate over the range and save images
for position in range(start, end + 1, step):
    # Set the slice offset
    red_logic.SetSliceOffset(position)

    # Define the file path
    filename = f"Red_Slice_{position}mm.png"
    filepath = os.path.join(output_directory, filename)

    # Save the screenshot
    saveScreenshot(red_logic, filepath)

    # Optional: Print the file path to confirm saving
    print("Saved:", filepath)

print("All slices saved successfully.")

```
