Taking screenshot of a specific area from the command line

In Lubuntu, you can do exactly what you want: take a screen shot from the command line with the command:

scrot_extended 100 100 400 400

using the script below.

The four arguments are <x>, <y>, <width>, <height>.
I didn't have the chance (yet) to test it in Lubuntu 12.04, but it seems unlikely it wouldn't work; it uses python 2 and basic command line tools that exist for a long time already.

Explanation

The script:

  • takes a screenshot with scrot
  • saves it into a temporary file
  • using imagemagick, it creates a new image, cropping the screen shot, with the arguments you ran the script with
  • the image is saved into a directory as a numbered file, to prevent overwriting

How to use

  1. The script uses both scrot and imagemagick. scrot should be on your system. To install imagemagick:

     sudo apt-get install imagemagick
    
  2. Copy the script into an empty file

  3. By default, images are saved to ~/scrot_images, named: outputfile_1.png, outputfile_2.png etc. . Change it if you want, as marked in the script. Note that if you change the diretory, you have to use the full path.

  4. Save the file to ~/bin (create the directory if needed) as scrot_extended (no extension) and make it executable.

  5. Log out and back in and take your screenshot with the command:

     scrot_extended <x> <y> <width> <height>
    

Example:

scrot_extended 100 100 400 400

enter image description here

outputfile:

enter image description here

The script

#!/usr/bin/env python
import subprocess
import os
import sys
# setting default directories / filenames
home = os.environ["HOME"]
temp = home+"/"+".scrot_images"
img_in = temp+"/in.png"
# if you prefer, you can change the two line below:
output_directory = home+"/"+"scrot_images" # output directory
filename = "outputfile"                    # filename
# creating needed directories
for dr in [temp, output_directory]:
    if not os.path.exists(dr):
        os.mkdir(dr)
# creating filename (-number) to prevent overwriting previous shots
n = 1
while True:
    img_out = output_directory+"/"+filename+"_"+str(n)+".png"
    if os.path.exists(img_out):
        n = n+1
    else:
        break
# reading arguments,arranging commands to perform
coords = sys.argv[1:5]
cmd1 = ["scrot", img_in]
cmd2 = ["convert", img_in, "-crop", coords[2]+"x"+coords[3]+"+"+coords[0]+"+"+coords[1], "+repage", img_out]
# Take screnshot, crop image
for cmd in [cmd1, cmd2]:
    subprocess.call(cmd)

Using maim


Overview

maim (make image) is a new screenshot utility that is designed as an improved version of scrot.

One of the many new features maim comes with is the support for setting a screen capture area right from the CLI. The syntax works as follows:

maim -x <x-coordinate> -y <y-coordinate> -w <width> -h <height>

e.g.:

maim -x 100 -y 100 -w 400 -h 400

Installation

maim hasn't arrived in the official repositories, yet, and isn't part of any PPA, either. You will have to compile it from source in order to install it.

After making sure that all dependencies are satisfied...

sudo apt-get install build-essential cmake
sudo apt-get install libimlib2-dev libxrandr-dev libxfixes-dev

...we can proceed with the actual compilation and installation:

git clone https://github.com/naelstrof/maim.git
cd maim
cmake ./
make && sudo make install

That's it. You should now be able to call maim from your terminal. Please make sure to check out the documentation (maim --help) for all available options; and check out slop, a utility by the same developer that allows you to interactively select the area of the screen capture.