April 2, 2007

HOWTO: Use xwd for screenshots

Filed under: Blog — krkhan @ 10:22 pm

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 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:


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.


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.


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: , , , , , , , ,