Saturday, February 10, 2024

How to take a screenshot using ssh on Wayland Desktop

On debian 11 for raspberrypi and older versions, we can take a screenshot of the active desktop from SSH by using scrot package and setting DISPLAY environment variable to the display number which is 0.0 by default:

        DISPLAY=:0.0 scrot -o screenshot.jpg

or in python:

        os.environ["DISPLAY"] = ":0.0"

        os.system("scrot -o {}".format(picfile))

On Debian 12  for raspberrypi and later versions, Wayland is the default window manager and scrot won't work. grim package can be used which is installed by default. A similar method can be used to access the default wayland desktop by setting WAYLAND_DISPLAY and XDG_RUNTIME_DIR environment variables :

WAYLAND_DISPLAY=wayland-1 XDG_RUNTIME_DIR=/run/user/1000 grim screenshot.png

os.environ["WAYLAND_DISPLAY"] = "wayland-1"

os.environ["XDG_RUNTIME_DIR"] = "/run/user/1000"

os.system("grim {}".format(pngfile))

To find the correct values for these environment variables, use env command in your main desktop to find which values are set for these. without setting proper values, you get the following error when trying to get a screenshot on a non-active tty:

failed to create display

No comments:

Post a Comment

How to disable Debian 12 sleep on production servers

 Debian 12 has power saver enabled by default which causes your server to go to sleep if there is no mouse / keyboard interaction. To resolv...