2

I'm trying to write a python script in ubuntu 16.04 LTS, that grabs a currently open terminal and take a screen-shot of it.

Most of the already available solutions (For eg. here) simply captures the screen-shot of the entire screen.

Pritam Pathak
  • 21
  • 1
  • 5
  • And why does it not work? What have you tried? – M. Becerra Mar 03 '18 at 11:56
  • I long ago used (bash script) a program that saved the window to file; so maybe you could shell outside python and use external program? (sorry I can't recall the project or command used..) – guiverc Mar 03 '18 at 11:58
  • I do not know how to grab that terminal window. The link that I mentioned takes the screenshot of the entire screen. I only need the screenshot of the terminal. – Pritam Pathak Mar 03 '18 at 12:23

1 Answers1

4
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", (), ())
Ping Chu Hung
  • 892
  • 4
  • 11