Search code examples
pythonlinuxpyqt5screenshotx11

How do I take a screenshot of a specfic window in Qt (Python, Linux), even if the windows are overlapped?


I'm trying to take a screenshot of the current active window in PyQt5. I know the generic method to take an screenshot of any window is QScreen::grabWindow(winID), for which winID is an implementation-specific ID depending on the window system. Since I'm running X and KDE, I plan to eventual use CTypes to call Xlib, but for now, I simply execute "xdotool getactivewindow" to obtain the windowID in a shell.

For a minimum exmaple, I created a QMainWindow with a QTimer. When the timer is fired, I identify the active window ID by executing "xdotool getactivewindow", get its return value, call grabWindow() to capture the active window, and display the screetshot in a QLabel. On startup, I also set my window a fixed 500x500 size for observation, and activate Qt.WindowStaysOnTopHint flag, so that my window is still visible when it's not in focus. To put them together, the implementation is the following code.

from PyQt5 import QtCore, QtGui, QtWidgets
import subprocess


class ScreenCapture(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
        self.setFixedHeight(500)
        self.setFixedWidth(500)

        self.label = QtWidgets.QLabel(self)

        self.timer = QtCore.QTimer(self)
        self.timer.setInterval(500)
        self.timer.timeout.connect(self.timer_handler)
        self.timer.start()

        self.screen = QtWidgets.QApplication.primaryScreen()

    @QtCore.pyqtSlot()
    def timer_handler(self):
        window = int(subprocess.check_output(["xdotool", "getactivewindow"]).decode("ascii"))
        self.screenshot = self.screen.grabWindow(window)

        self.label.setPixmap(self.screenshot)
        self.label.setFixedSize(self.screenshot.size())


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    window = ScreenCapture()
    window.show()
    app.exec()

To test the implementation, I started the script and clicked another window. It appears to work without problems if there is no overlap between my application window and the active window. See the following screenshot, when Firefox (right) is selected, my application is able to capture the active window of Firefox and display it in the QLabel.

When Firefox (right) is selected, my application is able to capture the active window of Firefox and display it in the QLabel.

However, the screenshot doesn't work as expected if there is an overlap between the application window and the active window. The window of the application itself will be captured, and creates a positive feedback.

If there is an overlap between the application window and the active window. The window of the application itself will be captured, and creates a positive feedback.

I've already disabled the 3D composite in KDE's settings, but the problem remains. The examples above are taken with all composite effects disabled.

Question

  1. Why isn't this implementation working correctly when the application window and the active window are overlapped? I suspect it's an issue caused by some forms of unwanted interaction between graphics systems (Qt toolkit, window manager, X, etc), but I'm not sure.

  2. Is it even possible solve this problem? (Note: I know I can hide() before the screenshot and show() it again, but it doesn't really solve this problem, which is taking a screenshot even if an overlap exists.)


Solution

  • As pointed out by @eyllanesc, it appears that it is not possible to do it in Qt, at least not with QScreen::grabWindow, because grabWindow() doesn't actually grab the window itself, but merely the area occupied by the window. The documentation contains the following warning.

    The grabWindow() function grabs pixels from the screen, not from the window, i.e. if there is another window partially or entirely over the one you grab, you get pixels from the overlying window, too. The mouse cursor is generally not grabbed.

    The conclusion is that it's impossible do to it in pure Qt. It's only possible to implement such a functionality by writing a low-level X program. Since the question asks for a solution "in Qt", any answer that potentially involves deeper, low-level X solutions are out-of-scope. This question can be marked as resolved.

    The lesson to learn here: Always check the documentation before using a function or method.


    Update: I managed to solve the problem by reading the window directly from X via Xlib. Somewhat ironically, my solution uses GTK to grab the window and sends its result to Qt... Anyway, you can write the same program with Xlib directly if you don't want to use GTK, but I used GTK since the Xlib-related functions in GDK is pretty convenient to demonstrate the basic concept.

    To get a screenshot, we first convert our window ID to an GdkWindow suitable for use within GDK, and we call Gdk.pixbuf_get_from_window() to grab the window and store it in a gdk_pixbuf. Finally, we call save_to_bufferv() to convert the raw pixbuf to a suitable image format and store it in a buffer. At this point, the image in the buffer is suitable to use in any program, including Qt.

    The documentation contains the following warning:

    If the window is off the screen, then there is no image data in the obscured/offscreen regions to be placed in the pixbuf. The contents of portions of the pixbuf corresponding to the offscreen region are undefined.

    If the window you’re obtaining data from is partially obscured by other windows, then the contents of the pixbuf areas corresponding to the obscured regions are undefined.

    If the window is not mapped (typically because it’s iconified/minimized or not on the current workspace), then NULL will be returned.

    If memory can’t be allocated for the return value, NULL will be returned instead.

    It also has some remarks about compositing,

    gdk_display_supports_composite has been deprecated since version 3.16 and should not be used in newly-written code.

    Compositing is an outdated technology that only ever worked on X11.

    So basically, it's only possible to grab a partially obscured window under X11 (not possible in Wayland!), with a compositing window manager. I tested it without compositing, and found the window is blacked-out when compositing is disabled. But when composition is enabled, it seems to work without problem. It may or may not work for your application. But I think if you are using compositing under X11, it probably will work.

    from PyQt5 import QtCore, QtGui, QtWidgets
    import subprocess
    
    
    class ScreenCapture(QtWidgets.QMainWindow):
        def __init__(self):
            super().__init__()
            self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
            self.setFixedHeight(500)
            self.setFixedWidth(500)
    
            self.label = QtWidgets.QLabel(self)
            self.screen = QtWidgets.QApplication.primaryScreen()
    
            self.timer = QtCore.QTimer(self)
            self.timer.setInterval(500)
            self.timer.timeout.connect(self.timer_handler)
            self.timer.start()
    
        @staticmethod
        def grab_screenshot():
            from gi.repository import Gdk, GdkX11
    
            window_id = int(subprocess.check_output(["xdotool", "getactivewindow"]).decode("ascii"))
    
            display = GdkX11.X11Display.get_default()
            window = GdkX11.X11Window.foreign_new_for_display(display, window_id)
    
            x, y, width, height = window.get_geometry()
            pb = Gdk.pixbuf_get_from_window(window, 0, 0, width, height)
    
            if pb:
                buf = pb.save_to_bufferv("bmp", (), ())
                return buf[1]
            else:
                return
    
        @QtCore.pyqtSlot()
        def timer_handler(self):
            screenshot = self.grab_screenshot()
            self.pixmap = QtGui.QPixmap()
            if not self.pixmap:
                return
    
            self.pixmap.loadFromData(screenshot)
            self.label.setPixmap(self.pixmap)
            self.label.setFixedSize(self.pixmap.size())
            
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication([])
        window = ScreenCapture()
        window.show()
        app.exec()
    

    Now it captures an active window perfectly, even if there are overlapping windows on top of it.

    Now it captures an active window perfectly, even if there are overlapping windows on top of it.