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.

  • And why does it not work? What have you tried?
    – M. Becerra
    Mar 3, 2018 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 3, 2018 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. Mar 3, 2018 at 12:23

1 Answer 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", (), ())
  • requires pip install vext.gi
    – Ivy
    Apr 26, 2020 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.