読者です 読者をやめる 読者になる 読者になる

Fedora Advent Calendar day 4

fedora

Take a screenshot from terminal.
You can do it with two commands that one is xwininfo and the other is import command. The import command take a screen shot but if you want to take screen shot particular window, you need to know its window id. If you will take whole display picture, you don't have to. So how do I know window id? The xwininfo command does. This utility is in xorg-x11-utils that shows window information. so, taking screen shot with these command, firstly you get window id by xwininfo then pass the id to import command.
When you run xwininfo without any arguments, you can see following message.

[masami@rune:~]$ xwininfo

xwininfo: Please select the window about which you
          would like information by clicking the
          mouse in that window.

When you click a window, xwininfo displays its information.

[masami@rune:~]$ xwininfo

xwininfo: Please select the window about which you
          would like information by clicking the
          mouse in that window.

xwininfo: Window id: 0x1800004 "LXTerminal"

  Absolute upper-left X:  0
  Absolute upper-left Y:  23
  Relative upper-left X:  0
  Relative upper-left Y:  23
  Width: 1366
  Height: 719
  Depth: 32
  Visual: 0x5d
  Visual Class: TrueColor
  Border width: 0
  Class: InputOutput
  Colormap: 0x1800003 (not installed)
  Bit Gravity State: NorthWestGravity
  Window Gravity State: NorthWestGravity
  Backing Store State: NotUseful
  Save Under State: no
  Map State: IsViewable
  Override Redirect State: no
  Corners:  +0+23  -0+23  -0-26  +0-26
  -geometry 170x47+0+0

In the output, window id is 0x1800004 and window name is "LXTerminal". This is a simple way to use it. However if you know window name, you can give it to the command with -name option then you can get same output.
Once you get window id you can pass to import command to take a screen shot. Following command take a screen shot.

[masami@rune:~]$ import -window 0x1800004 a.png

That command line arguments is simple. It give window id and output file name. if you want to take whole window's screen shot, pass "root" for window id. This command has several command line options e.g. to make image monochrome.
I wrote simple python script to do this. You can download it from here.