Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture screen on Wayland(Gnome) in Python code?

I'm trying to capture my screen using Python because I'll use it on OpenCV, but I couldn't find a way to make it work on Gnome, since Gnome uses Wayland and all libraries that I've found only work with X11.

For now I'm not considering change my interface. I'm searching a solution to this problem. Does someone know a solution?

To be more specific, I'll use the images to train an AI and so I need they continuously.

EDIT: I've found this but how can I pass frames to OpenCV in Python instead of save a video file?

like image 550
Pedro Vinicius Avatar asked Nov 08 '22 21:11

Pedro Vinicius


1 Answers

The proper way to do screencasting these days is by using the Screencast portal, which is part of XDG desktop portals and is already supported by GNOME, KDE, wlroots (and more). As an added advantage, this will also work in containerized formats like Flatpaks.

You can find an example on how to do screencasting in Pyhon using this snippet, created by one of the Mutter maintainers. If you look for parse_launch(), you will see a GStreamer pipeline which you can modify to include the GStreamer OpenCV elements that can do the processing for you.

Note: in your edit, you link to a predecessor of that portal, which is GNOME-specifc, internal API, so I wouldn't rely on it ;-)

like image 173
nielsdg Avatar answered Nov 14 '22 20:11

nielsdg