python-pillow / python-pillow/Pillow
Use xdg-desktop-portal for Image.grab() on Linux
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 13.8k
- Forks
- 2.5k
- Avg merge
- 2d 8h
- Merged PRs (30d)
- 89
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
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.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- linux, python
- Domain
- desktop, operating-systems
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100