On Linux there are many utilities we can use to take screenshots. Every complete desktop environment, such as GNOME, KDE or XFCE has its integrated application specifically designed for this task, but many other small
desktop-independent programs exist. In this tutorial we talk about a very lightweight and versatile command-line application, maim (make image), and we see what are the many options we can use to modify its behavior.
In this tutorial you will learn:
- How to install maim on the most used Linux distributions
- How to take a screenshot of all the screen
- How to save the screenshot in a specific format (png/jpg)
- How to select a region of the screen interactively
- How to take a screenshot of a window by passing its id to maim
- How to take screenshots with a delay
- How to use maim in a pipeline
Software requirements and conventions used
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Distribution independent |
Software | maim, xdotool to get windows id |
Other | none |
Conventions | # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux-commands to be executed as a regular non-privileged user |
Installation
The maim utility was designed as an improved version of another command line utility used to take screenshots on Linux: scrot. The application is free and open source software, and the source code is available on github. Maim is available in the default repositories of all the most used Linux distributions, so we can easily install it on Fedora, Archlinux, Debian and its derivatives. On the community distribution sponsored by Red Had, we can use the dnf
package manager to accomplish the task.
We run:
$ sudo dnf install maim
The “maim” package is also available in the Archlinux “Community” repository. To install it, we can use pacman
:
$ sudo pacman -Sy maim
Finally, on Debian and its derivatives we can synchronize our repositories and install the utility using apt-get
:
$ sudo apt-get update && sudo apt-get install maim
Once the application is installed in our system, we can use it to take screenshots from the command line. Let’s see how!
Basic usage
The maim utility is very easy to use. First of all let’s see its basic usage. Let’s say we want to take a screenshot of the entire screen and save it to the “screenshot.png” file. All we have to do is to invoke maim and pass the path
of the file as argument. We run:
$ maim ~/screenshot.png
By default the application will try to understand the format in which to save the image from the filename, so for example, when running the above command the image will be saved as a png. There is, however, an option we can use to explicitly pass the format we want to use: -f
(short for --format
). To explicitly save an image
as jpg, we would run:
$ maim -f jpg ~/screenshot
As we already said, only the png and jpg formats are supported, the former being the default one. We have also the possibility to select the quality of the resulting image using the -m
option and express the compression level with an integer from 1
to 10
: this will have different effects depending on the chosen image format.
Interactively selecting the region to capture
When running the commands we used in the previous examples, the entire screen content will be included in the screenshot automatically, without the need of user interaction. In some cases, however, we may want to select the regions of the screens to capture more precisely. To accomplish the task, we can run the application with the -s
option (--select
); this will run maim in “interactive mode”:
$ maim -s ~/screenshot
Once we run the command above, the cursor shape will change into a “plus” sign and we will be able to select the region to capture. Once we release the mouse left button the screenshot will be saved to the specified path.
Capturing only a specific window by its id
Another option we can use to capture a specific window, leaving out all the rest, is to run maim with the -i
(--window
) option, and pass the id of the window that we want to capture as argument to the option. At this point a question will surely raise: “How can we get a window id ?”. When using the Xorg server, we can use the xdotool
utility to accomplish this task. All we have to do is to invoke the application with the selectwindow
command and then, once the cursor shape changes, click on the window we want to know the id of. Its id will be reported in the terminal:
$ xdotool selectwindow 37748743
Once we know the id of the window, we can pass it to maim, in the following way:
$ maim -i 37748743 ~/screenshot
We can use the same principle to capture only the active window. This time all we have to do is to launch xdotool with the getactivewindow
command, which returns the id of the current active window. Using the shell command
substitution feature, we can do everything in a single line:
$ maim -i "$(xdotool getactivewindow)" ~/screenshot
Including the background when a specific region is selected
When we select a specific region of the screen to be captured, either interactively or by specifying the window id as in the previous example, maim will only capture the specified window and will ignore the background. Here is
an example of a screenshot taken selecting the current active window:
As you can see, the original background that was under the image was not included in the screenshot. In the vast majority of cases this is the wanted behavior. If we want to include the background, however, we can use the -B
option, which is the short form of --capturebackground
. Below you can see the result of invoking the application with it:
Taking a screenshot with a delay
Maim comes with a very interesting functionality: it can use a delay expressed in seconds before actually taking a screenshot. The option that allow us to do that is -d
(which is the short for --delay
). As you can imagine all we have to do is to pass a float as argument to the option. To wait 5
seconds before actually taking a screenshot we can therefore run the following command:
$ maim -d 5 ~/screenshot
Once the command is launched a countdown will appear on the screen. After it expires, the screenshot will be saved to the specified location.
Using maim in a pipeline
In the previous examples we provided a file path which should be used to save the screenshot taken with maim. This argument, however, can be omitted if maim is used in a pipeline, since the command by default outputs the encoded image data to the standard output. This feature allow us to do something with the image taken with maim on the fly. Let’s see an example. Suppose we want to take a screenshot and automatically copy it to our clipboard; if using Xorg, we could write:
$ maim | xclip -selection clipboard
In the example above the output of maim, which is the encoded image data, is used as the standard input of the xclip
application, which is the command line interface to the X server selections. In this case we run it with the -selection
option which let us specify which X selection to use.
The encoded image data will be saved in our clipboard, ready to be pasted in our favorite image editor or any other application which supports images.
In the same fashion we modify the taken image by passing it to the standard input of command line applications such as ImageMagick.
Conclusions
In this tutorial we learned how to use maim, a command line utility used to capture screenshots on Linux when running the Xorg server. We saw how to install the application in some of the most used Linux distributions, how to
use it to capture the entire screen or only a specific region interactively or by passing the application the id of a window and how to specify the format in which to save the screenshot. We also saw how take screenshots with a delay, and how to run maim in pipeline.