X-Window: Screenshots

October 2016

How to take a screen capture when no screenshot tool (Ksnapshot, ...) is installed on your system?
The ImageMagick* package (binary available for most distributions **) contains a dozen commands dedicated to image manipulation.
The one that interests us here is the import command.

The import command

import can be called in 2 ways:
  • $ import file.ext
  • $ import -window WinId file.ext

I invite you to consult the manual (man 1 import) to learn about the other options of this command.

The first call means that you want to capture and store a screenshot in the file.ext
  • Where .ext defines the format of image compression: png, gif, jpg, etc..

Once the command issued, the pointer turns into a cross. We then have two possibilities:
  • Define an area to capture with a click and drag
  • Define a window to capture by clicking on the border.

The second call, allows you to specify the window you want to capture through its name or ID.
Where the "root" parameter defines the Desktop.


Knowing this, it is possible to generate a small script to facilitate the use of the import command:
#{{{ Vérification de l'existence du binaire import
if [ ! -x ${import} ]; then
  echo "${import} not found!"
  exit 1

#{{{ Création du dossier "captures" si besoin
mkdir -p ~/captures

#{{{ Génération du nom de la capture
name=$(date +"%Y.%m.%d-%H.%M.%S")

#{{{ Traitement (basique) de l'argument
if [ $# -eq 1 ]; then
  option=" -window $1"

#{{{ Capture
$import $option ~/captures/$name.png

The purpose of this script:
  • Verify the presence of the binary.
  • Creating a folder to store your screenshots in the home directory of the user.
  • Capture the:
    • entire screen if the "root" option is given,
    • the desired window if its name or Id is passed as a parameter,
    • a particular area with the mouse if no argument,
  • Save the screenshot in the "png" format as: year.month.day-hours.minutes.seconds.png

All that remains is to configure the following keyboard shortcuts (KHotKeys under KDE)
  • <alt> + <print> -> capture.sh
  • <print> -> capture.sh root


Related :

This document entitled « X-Window: Screenshots » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.