# 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:** 1\
**Showing post:** 3

<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

---

_[View the full topic](https://discourse.slicer.org/t/need-help-for-a-script-of-saving-screenshot-for-many-times/32877)._
