我正在尝试在 ubuntu 16.04 LTS 中编写一个 python 脚本,抓取当前打开的终端并对其进行屏幕截图。
大多数现有的解决方案(例如这里)只是捕获整个屏幕的屏幕截图。
答案1
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gdk
# full screenshot
window = Gdk.get_default_root_window()
pb = Gdk.pixbuf_get_from_window(window, *window.get_geometry())
pb.savev("full.png", "png", (), ())
# screenshots for all windows
window = Gdk.get_default_root_window()
screen = window.get_screen()
typ = window.get_type_hint()
for i, w in enumerate(screen.get_window_stack()):
pb = Gdk.pixbuf_get_from_window(w, *w.get_geometry())
pb.savev("{}.png".format(i), "png", (), ())
# screenshot active window
screen = Gdk.get_default_root_window().get_screen()
w = screen.get_active_window()
pb = Gdk.pixbuf_get_from_window(w, *w.get_geometry())
pb.savev("active.png", "png", (), ())