def grab(widget, imagePath):
    GafferUI.EventLoop.waitForIdle()
    imageDir = os.path.dirname(imagePath)
    if (imageDir and (not os.path.isdir(imageDir))):
        os.makedirs(imageDir)
    if (Qt.__binding__ in ('PySide2', 'PyQt5')):
        screen = QtWidgets.QApplication.primaryScreen()
        windowHandle = widget._qtWidget().windowHandle()
        if windowHandle:
            screen = windowHandle.screen()
        pixmap = screen.grabWindow(long(widget._qtWidget().winId()))
        if ((sys.platform == 'darwin') and (pixmap.size() == (screen.size() * screen.devicePixelRatio()))):
            topLeft = widget._qtWidget().mapToGlobal(QtCore.QPoint(0, 0))
            bottomRight = widget._qtWidget().mapToGlobal(QtCore.QPoint(widget._qtWidget().width(), widget._qtWidget().height()))
            size = (bottomRight - topLeft)
            pixmap = pixmap.copy(QtCore.QRect((topLeft * screen.devicePixelRatio()), (QtCore.QSize(size.x(), size.y()) * screen.devicePixelRatio())))
    else:
        pixmap = QtGui.QPixmap.grabWindow(long(widget._qtWidget().winId()))
    pixmap.save(imagePath)
def grab(widget, imagePath):
    GafferUI.EventLoop.waitForIdle()
    imageDir = os.path.dirname(imagePath)
    if (imageDir and (not os.path.isdir(imageDir))):
        os.makedirs(imageDir)
    if (Qt.__binding__ in ('PySide2', 'PyQt5')):
        screen = QtWidgets.QApplication.primaryScreen()
        windowHandle = widget._qtWidget().windowHandle()
        if windowHandle:
            screen = windowHandle.screen()
        pixmap = screen.grabWindow(long(widget._qtWidget().winId()))
        if ((sys.platform == 'darwin') and (pixmap.size() == (screen.size() * screen.devicePixelRatio()))):
            topLeft = widget._qtWidget().mapToGlobal(QtCore.QPoint(0, 0))
            bottomRight = widget._qtWidget().mapToGlobal(QtCore.QPoint(widget._qtWidget().width(), widget._qtWidget().height()))
            size = (bottomRight - topLeft)
            pixmap = pixmap.copy(QtCore.QRect((topLeft * screen.devicePixelRatio()), (QtCore.QSize(size.x(), size.y()) * screen.devicePixelRatio())))
    else:
        pixmap = QtGui.QPixmap.grabWindow(long(widget._qtWidget().winId()))
    pixmap.save(imagePath)