DE

Screenshots in Sway

To take screenshots in Sway, grim, grimshot and slurp were developed. Moreover, with wl-clipboard screenshots can be saved directly to the clipboard. These tools are already preinstalled in the Fedora Sway Spin.

This article discusses some examples that illustrate how to make use of these programs. The goal is that in the end we will be able to use keyboard shortcuts to take screenshots in predefined ways.

Grim: The Main Tool

The central tool for taking screenshots in Sway is grim. Here is one example of how to use it:

grim -g "10,20 300x400" # Take screenshot of an area
grim -g "$(slurp)" # Let another tool provide the area

The first line shows the format in which a screen area is to be specified. With slurp we can select an area interactively with the mouse; the output corresponds exactly to the format expected by grim. We can pass the output as an argument by inserting it via command substitution ("$(...)").

grim creates an image file whose name contains a timestamp and is located in ~/Pictures by default, though it may be changed using the $GRIM_DEFAULT_DIR environment variable. An output can be explicitly specified with grim -o [FILE].

In addition, grim can send the result directly to the standard output (stdout). In this way, we can transfer it to the clipboard, for example by using wl-copy:

grim - | wp-copy

This makes it easy to insert the screenshot into a suitable application such as GIMP.

grim, slurp and ImageMagick can be used together to extract the color of a clicked pixel (color picker), which may be helpful for web developers.

grim -g "$(slurp -p)" -t ppm - | magick - -format '%[pixel:p{0,0}]' txt:-

Here, a pixel is selected with slurp, grim creates a screenshot in ppm format, and magick analyzes the color of the pixel. The result is displayed in the terminal.

Grimshot: Modalities

Grimshot is a helpful script that abstracts away from some of the details of using grim. Instead, it provides a simplified interface to specify modalities. Here is the synopsis with the most interesting components:

grimshot [--cursor] [--wait N] (copy|save) [TARGET] [FILE]

The --cursor flag is used to include the mouse cursor as visible in the screenshot. With --wait N you can specify after how many seconds the screenshot should be taken. copy or save ... [FILE] are used to specify whether the screenshot is to be saved to the clipboard or to a file.

Grimshot refers to the screenshot subject as [TARGET]. Four options are of particular interest.

  • In the simplest case, a screenshot is taken of the entire visible area (screen).
  • A variable screen area can be selected with the mouse (area).
  • A screenshot can be taken of a window: either of the active window (active) or of a window that is selected with a mouse click (window).

The following command would save a screenshot of the active window in a file in the user directory:

grimshot save active ~/screenshot.png

Usage

The commands mentioned are not primarily designed to be entered and executed manually. For convenient use, it’s best to define key mappings in the Sway configuration. This is the general format:

bindsym <key combination> exec <command>

The configuration could look like this:

# Take screenshots:
    set $screenshot_out $(xdg-user-dir PICTURES)/screenshots/screenshot-$(date +"%Y%m%d-%H%M%S").png
    bindsym $mod+Shift+s exec grimshot save screen $screenshot_out
    bindsym $mod+Shift+w exec grimshot save active $screenshot_out
    bindsym $mod+Shift+a exec grimshot save area $screenshot_out

First, a variable is used to specify the name and location of the image file to be created. In the example, these are ~/Pictures/screenshots and screenshot-yyyymmdd-hhmmss.png, respectively. The format selected for the timestamp is relatively easy to read and allows chronological sorting by file name. Key combinations are then mapped to some of the commands explained above.

Automatic Conversion

Screenshots can be converted directly into other formats by piping the grim output into ImageMagick. For web applications, for example, WebP is recommended as target format.

To transfer the created screenshot to the standard output (stdout), - is set as the file name. ImageMagick receives the input via stdin, which is also denoted by -. The desired format is recognized from the file extension.

An example command for converting to WebP format could look like this:

grimshot save screen - | magick - -quality 85 $(xdg-user-dir PICTURES)/out.webp

The automatic conversion can be integrated directly into the Sway configuration, so that screenshots are always stored in a specific format, as in this example:

set $screenshot_out $(xdg-user-dir PICTURES)/screenshots/screenshot-$(date +"%Y%m%d-%H%M%S").webp
bindsym $mod+Shift+s exec grimshot save screen - | convert - -quality 85 $screenshot_out

Article from September 6, 2024.