HOWTO: Use xwd for screenshots
If you’re using KDE, KSnapshot can do the job for you. If you’re using GNOME, gnome-screenshot can also achieve the task with quite perfection. So why would someone ever need tinkering with command-line utilities to generate screenshots?
The answer is: xwd
is useful because not everyone uses KDE or GNOME and those who don’t usually despise installing needless extra applications for something as trivial as capturing a screenshot. xwd
comes with the X.org server itself, while almost all distributions by default provide the necessary software needed to convert xwd
screenshots into more popular formats. Moreover, KSnapshot and gnome-screenshot have a plethora of dependencies on their relative desktop environments whereas xwd
is a small, neat and efficient solution for capturing. Let’s start with the requirements:
Lights
The first thing that you need to be sure of is that you have either ImageMagick or Netpbm installed on your system. The quickest (generic) way for checking their existence is to issue man ImageMagick
and man netpbm
commands in a terminal. If you receive “No manual entry for …” errors for both commands, head over to your distribution’s website and download a package for ImageMagick since it would be relatively easier to find.
Camera
If you want to capture the whole screen, you can safely skip this section. However, if you want to target a particular window, some extra information shall be needed. Open a terminal and issue the command:
$ xwininfo
xwininfo: Please select the window about which you
would like information by clicking the
mouse in that window.
Click on the window for which you’d like to take the screenshot. You’ll be shown lots of details about your choice. Only the first line is important. It would be containing a window id. Something like:
xwininfo: Window id: 0x1a00003 “1. Rammstein – Mutter – Ich will (3:39) – Audacious”
Copy the window id. You’ll be needing it in the next section.
Action
Now comes the real part. If you want to capture the whole screen and have ImageMagick installed, use the following command:
$ xwd -root | convert xwd:- capture.png
A file named ‘capture.png’ shall be created in your current directory. You can change the png extension in the command to generate a variety of other formats (e.g. jpg, gif, tiff).
To capture a particular window, you have to provide the window id that xwininfo
provided you. The final command will look something like:
$ xwd -id window id | convert xwd:- capture.png
Someone might start wondering here why didn’t I use import command from the ImageMagick suite. Actually, the problem with import is that it really doesn’t work well with all compositing window managers. For example, Emerald’s decorations are always chopped off whenever I take a screenshot with it.
If you have Netpbm installed, you can use these commands instead:
$ xwd -root | xwdtopnm | pnmtopng > capture.png
$ xwd -id window id | xwdtopnm | pnmtopng > capture.png
Finally, if you don’t like the terminal window in full-screen captures, you can insert sleep 3;
before any command given above to give yourself some time for minimizing it.
Happy capturing!
Tags: Command-line, gnome-screenshot, ImageMagick, KSnapshot, Netpbm, Open Source, Screenshot, Tutorial, xwd
Here is a simple little script that grabs the “Window id:”. Run the script and click on the window that you want the capture. You might want to fine tune the options for xwd.
#! /bin/bash
WindowID=`xwininfo | grep “Window id:” | awk ‘{print $4}’`
#echo $WindowID
xwd -id $WindowID | convert xwd:- capture.png
Hope this helps some one.
Comment by Pete — September 17, 2009 @ 4:25 am
Since convert automatically recognizes XWD files (via their magic number), you can just use something like
$ xwd -id window id | convert – capture.png
Comment by Andre Robatino — February 6, 2012 @ 11:55 pm
When I tried Pete’s script it would have black boxes over the window I clicked if it was behind other windows, so I made a modified version, using wmctrl to bring the window to the front before taking the cap :
#!/bin/bash
WindowName=`xwininfo | grep “Window id:” | awk -F\” ‘{print $2}’`
wmctrl -a “$WindowName”
xwd -name “$WindowName” | convert xwd:- capture.png
Comment by mwic — May 22, 2013 @ 10:06 pm
Why all that hassle just to get the window id? If I run xwd without “-id” it turns the mouse pointer into an arrow that I can then use to select a window.
Comment by karl — February 28, 2015 @ 3:25 pm