Hack 12. Take a Screenshot from the Command Line
Who needs a graphical tool to grab a screenshot? The command line has everything you need.
For many writers and programmers, screenshots are useful for showcasing how an interface or program looks. Although grabbing a screenshot is often as simple as running a small utility or clicking an option, some grabbers are not as flexible as you need them to be. For example, sometimes you might need a screenshot without the screenshot tool displayed in the taskbar, or you might need to take a screenshot in an environment, where you cannot run a graphical screenshot utility. This is a common problem for those who need to take screenshots of installation programs or software on embedded devices.
2.4.1. Take a Screenshot from an X Terminal
Although a graphical screenshot-grabbing tool is the obvious choice for making a screenshot, most of these utilities leave a trace of themselves in the screenshot by having an entry on the taskbar or being visible on the desktop. You can solve this problem by using a collection of command-line tools to take the screenshot from an X terminal or even from within the Run option in the KDE/GNOME main menu:
foo@bar:~$ sleep 2; import -window root screen.png
This command is actually composed of two separate utilities. The sleep command delays the process for two seconds before the screenshot is taken. This gives you time to minimize windows, expand menus, or make other necessary adjustments before the screenshot is taken. By changing the sleep value you can control the delay before the screenshot is taken. The second command uses the import utility that is part of the ImageMagick suite of tools (use your package manager to install ImageMagick if it isn't already on your system) to take a screenshot of the root window (the root window is the entire screen) and name the image screen.png. If you want to grab a particular part of the screen, you also can use the -crop option to grab that specific area (such as import -crop 500x400). And if you run import without -window your cursor will change to a crosshair, which you can drag over the area you want to capture. For many print and digital media houses, PNG is a recommended screenshot format, but ImageMagick supports a range of different formats, so you can use what you need. Read the import manpage for more information.
2.4.2. Take a Screenshot from a Command-Line Terminal
If you need to take a screenshot from a command-line console while X is running elsewhere on the system, adjust the command line and add a few additional features to it. This method is commonly used when you need a screenshot of an installation routine or a program running on an embedded device. To do this, first access the shell that runs behind the installer by pressing Ctrl-Alt-F2; this provides you with a simple shell in which you can run the commands to grab the shot. If you are planning on using this hack while installing a Linux distribution, you might need to copy the chvt, sleep, and import commands onto a floppy disk so that you can mount it and access the programs. You can mount the floppy disk with:
foo@bar:~$ mount /dev/fd0 /mnt/floppy
Before you can run the command to grab the shot, you need to find out the display number of the running X server. Every X server has a unique display number that is mapped to the particular user who is using X. This number can be used to distinguish between different X displays. You can find this number by checking the $DISPLAY environmental:
foo@bar:~$ echo $DISPLAY
Now you can run the command. For example, if your display number returns :0.0, the main command to grab the screenshot is:
foo@bar:$ chvt 7; sleep 2; import -window root screen.png-display :0.0 ; chvt 2
This command runs through the process of switching to the X terminal, grabbing the screenshot, and switching back to your current command-line terminal. The first command (chvt 7) switches to the X terminal (usually the 7th terminal), and then the second command (sleep 2) pauses the process for two seconds. This pause allows for the machine to switch to the 7th terminal before the screenshot is taken. Then the import command is used to grab the root window on display :0.0 and save the shot as screen.png. Finally, the terminal is switched back to terminal 2 (chvt 2). If you run this from a terminal other than 2, you need to change the number on the last command to the relevant terminal number. If you need to store the image to the floppy disk when you run this command, you need to prepend the filename with the mount point of the floppy diske.g., /mnt/floppy/screen.png.