Use xdg-desktop-portal for Image.grab() on Linux

Ouverte
#6,392 13 commentaires 2 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
5/5
Temps estimé
Plus d'une semaine
Accessibilité débutants
35/100
Type d'issue
Fonctionnalité
Clarté
À clarifier
Activité
À l'abandon
Stack technique
linux, python

Piste de recherche

Start at the Image.grab() entry point and read the xdg-desktop-portal Screenshot API linked in the issue. Evaluate a sustainable D-Bus approach and its response handling; done means Linux screenshots work across X11 and Wayland, including sandboxed and unsandboxed environments, with the saved location available to the program.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

Linux Screen grab

Making a Screenshot on Linux is little bit hacky. Especially on Wayland. @radarhere tried to solve the problem in #6312 by using gnome-screenshot, but this is not a good way. It works only on Systems with Gnome. Of course, you can also add other Screenshot tools but it is far more hacky and it will not work in Sandboxed Environments. Luckily there is a Better Way: There is a official D-Bus API for taking Screenshots. It works in every Desktop Environment in X11 and Wayland. It works also in Sandboxed and Unsandboxed Environments. Here is a Example code that uses it:

import dbus
import dbus.mainloop.glib
from dbus.mainloop.glib import DBusGMainLoop

from gi.repository import GLib


def testresp(response, results, object_path):
    print(results["uri"])


def main():
    loop = GLib.MainLoop()
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    bus = dbus.SessionBus()
    obj = bus.get_object("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop")
    inter = dbus.Interface(obj, "org.freedesktop.portal.Screenshot")
    bus.add_signal_receiver(
        testresp,
        signal_name="Response",
        dbus_interface="org.freedesktop.portal.Request",
        bus_name="org.freedesktop.portal.Desktop",
        path_keyword="object_path")
    inter.Screenshot("", {})
    loop.run()


main()

It needs the dbus-python. This Lib might not be the best choice, since it might be tricky to install and the mainloop can cause problems.

You can also use this API with the Shell:

gdbus call --session --dest org.freedesktop.portal.Desktop --object-path /org/freedesktop/portal/desktop --method org.freedesktop.portal.Screenshot.Screenshot "" {}

This makes a Screenshot, but I haven't found a Way to get the response, so the Program don't know where the Screenshot has been saved.

This Issue is a note that this API exists. If someone reading this with a better knowledge of D-Bus, he or she can write a better code in a more sustainable lib and make a PR. I might also take a closer look at this when I have some free time.

Langage dominant
Python
Étoiles
14k
Forks
2.6k
Merge moyen
1 j 1 h
PR mergées (30 j)
205

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de python-pillow/Pillow

Toutes les issues de python-pillow/Pillow

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.