12

I am running Lubuntu desktop 12 and I am looking for a tool that can take screenshots of a certain area from the command line.

I have tried out Shutter. It works but, when I run the command it generates warnings ( Wnck-WARNING **: Unhandled action type). I think the tool might be designed to run under GNOME and might not be compatible with Lubuntu. The screenshot is taken successfully, but the command hangs, which is something I cannot work with.

So what is a good screenshot tool that

  1. Runs from the command line
  2. Can capture a certain area of the desktop

I like to add that scrot, the tool Lubuntu ships with, does not have the option to crop certain coordinates, but only an interactively user-defined area which is not what I'm looking for.

2
  • 1
    Have you tried the --exit_after_capture option of shutter? Also, many GUI programs generate such warnings when run from the command line. Nothing to really worry about.
    – muru
    Feb 14, 2015 at 19:41
  • Thank you I read the man page, I did not come across this switch, even though the command takes some time after capturing to finish, but still it does. And so my script can continue
    – AL-Kateb
    Feb 15, 2015 at 14:56

6 Answers 6

9

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)
3
  • Thank you very much for the extended answer, I am not sure whether other tools "like shutter" which has the feature built in, do it same way. Do they shoot whole screen and then crop? I think not. Cause' you know it would be faster and more efficient than taking whole desktop screenshot and then cropping it. But either way this script is very handy, thanks again
    – AL-Kateb
    Feb 15, 2015 at 14:59
  • @AL-Kateb I am glad it works! I am not sure, but I assume other tools do take a shot of the whole screen and then process it, since the time Shutter takes to do the exact same job on my system is (much) longer. Feb 15, 2015 at 15:15
  • 1
    @JacobVlijm, if the same area is always to be captured, won't something like scrot /tmp/img-in.png -e "convert /tmp/img-in.png -crop 100X200+300+400 ~/Desktop/"$(date +%Y%m%d%H%M%S)".png" be enough?
    – DK Bose
    Jun 5, 2017 at 10:25
4

There is an easier way to do this using maim than with the accepted answer of scrot_extended. There is an answer on how to take fixed-size screenshots in maim which is outdated now.

maim allows you to specify the geometry using the geometry(g) flag. As referred to in the documentation:

 -g, --geometry=GEOMETRY
              Sets the region to capture, uses local coordinates from the given
              window. So -g 10x30-5+0 would represent the rectangle wxh+x+y where
              w=10, h=30, x=-5, and y=0. x and y are the upper left location of this rectangle.

Usage:

maim -g <w>x<h>+<x>+<y> <output-file>

Example:

maim -g 1024x1024+10+20 ~/test.png
1
  • 3
    And you can use slop to get those geometry coordinates interactively
    – Anthon
    Nov 7, 2021 at 7:34
3

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.

2
  • 1
    This does not seem to be current anymore. Version 5.4 of maim does not have the command line arguments -x, -y, -w and -h with the function that you describe. Instead, the --geometry option has to be used.
    – josch
    Dec 13, 2017 at 15:11
  • maim also seems to get really flaky after you change screen resolutions.
    – marinara
    Feb 13, 2020 at 22:07
3

The current version of scrot does have the ability to screenshot a defined rectangle off the screen.

scrot --autoselect '1,2,3,4' outfile.png # screenshot the rectangle at 1,2 with 3 width and 4 height

Naturally, you would replace 1,2 with the x,y pixel coordinates of the rectangle you want. So x,y would be the upper left pixel of the rectangle you desire.

also see this answer for instructions on how to get the current mouse coordinates from a bash script

1

Bind a bash script that implements scrot and imagemagick to a keyboard shortcut

This does nearly the same thing as Jacob Vlijm's answer but with bash. It names files using a timestamp to avoid overwriting existing files. It also allows you to define default cropping parameters in the script so you're not required to call it with any parameters.

Call the script below as follows (assuming you're in the directory where the script is stored, otherwise the full path to the script is required):

  • With parameters: ./screenshot.sh $left_px $top_px $width_px $height_px OR
  • Without parameters: ./screenshot.sh in which case, the default parameters specified in the script are used.

1) Install the necessary applications

From the command line, run:

sudo apt install scrot imagemagick

2) Create the script

Open your text editor of choice and create a new plaintext file with the following contents. Be sure the modify the variables at the top to specify where you want the images saved and what portion of the screen you want to crop out. See this trick for getting mouse coordinates which can be used to find left and top and to calculate width and height.

#!/bin/bash

# Change these values to match your preferences
imageQuality=100    # scrot default is 75
screenshotDir="/tmp"    # directory in which to save screenshots
imageName="$(date +%Y-%m-%d.%H:%M:%S.%N).jpg"   # save image names as timestamp
default_left=10     # begin crop this number of pixels from the left of the image
default_top=10      # begin crop this number of pixels from the top of the image 
default_width=100   # crop this many pixels wide
default_height=100  # crop this many pixels tall

#Do not make any more changes from here down unless you know what you're doing
l=$1; t=$2; w=$3; h=$4
left=${l:=$default_left}
top=${t:=$default_top}
width=${w:=$default_width}
height=${h:=$default_height}
imagePath="$screenshotDir/$imageName"

[ ! -d "$screenshotDir" ] && mkdir -p "$screenshotDir"
scrot -q $imageQuality "$imagePath"
convert "$imagePath" -crop ${width}x${height}+${left}+${top} "$imagePath"

Save this script wherever you like and make it executable. Assuming you named your script screenshot.sh, you would do this at the command line like so:

chmod +x /path/to/your/script/screenshot.sh

3) Bind this script to a keyboard shortcut (optional)

Follow the directions found here to create a custom keyboard shortcut. When you get to the point where you're supposed to enter the command, put the complete path to your screenshot.sh file (including the filename).

0

According to this document at the wiki here: https://wiki.ubuntu.com/Lubuntu/Applications/Process%20Documentation xfce4-screenshooter is one of the screenshot apps for that distro. On my XFCE setup the following command does what you're asking.

xfce4-screenshooter

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .