Wednesday, March 23, 2011

Linux: launch window, capture screen

I need to write a Red Hat Linux command line tool that launches a window and captures its appearance to disk as a JPEG.

Typically the target machines don't have graphics cards, but we can install any software components (e.g., X).

Question or two:

What libraries or tools might you suggest for this?

If I were to use something like GTK+ to create this tool, would lacking a video card hamper its execution?

I saw scrot, but it doesn't appear to support capturing a specific window without user interaction.

From stackoverflow
  • It sounds like you'll need to use the "virtual framebuffer" driver for the X.org server, combined with the xwd, NetPBM, and cjpeg utilities.

    I'm not sure about the particular configuration you'll need for the X server, but you will likely have to make sure the server you're using has the virtual framebuffer driver built into to. The virtual framebuffer driver is a display driver just like one you'd use to connect to an NVidia or ATI video card, except it's "output" is a chunk of memory that contains the pixels, not an LCD screen.

    xwd is one of the standard X tools, that can create a X Window Dump. xwd can be told on the command line which window to dump. It outputs a funky "xwd" formatted stream to standard out.

    The NetPBM utilities are a collection of command line tools that convert one image format to another. It includes one that converts xwdtoppm. PPM is a very basic, non-compressed format that is the intermediate format understood by most of the NetPBM tools.

    cjpeg is part of the standard JPEG tools collection, and is probably installed if you also have NetPBM. cjpeg can take a stream of PPM bytes and emit a stream of JPEG bytes.

    Through the magic of Unix scripting and pipes, you can string these utilities together to fire up the app with the window, call xwd, xwdtoppm, and cjpeg to dump the image to a file.

  • You might try running vncserver to create a virtual X window display - no graphics card needed. Be sure to set your DISPLAY variable to the display number that gets printed when vncserver starts. Next, start your app on the created display (in hte background) and use xwd with data formatters or a gimp command to capture the screen image to jpeg.

    By the way, check the similar answers for http://stackoverflow.com/questions/125951/command-line-program-to-create-website-screenshots-on-linux.

0 comments:

Post a Comment