Need help for a script of saving screenshot for many times

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

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

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

Still nothing shows up