Ubuntu – Is it possible to take a screenshot of a circular selected area

productivityprogrammingscreenshotscriptssoftware-recommendation

When we take screenshots of selected areas on Ubuntu (using Shift+Prt Scr or Ctrl+Shift+Prt Scr) we always select a rectangular area. So, I know I could just edit the image on some software like Inkscape to create a circle with the rectangular image, but I'd like to know if there's a configuration I can change for being able to select different kinds of shapes (mainly circles) directly when I'm taking the screenshot.

Is it possible?

Best Answer

I put together a small script for circular screenshots, you need the following packages (though it’s adaptable for other screenshot programs and terminal emulators as well):

sudo apt install xdotool gnome-screenshot imagemagick xterm

The script

#!/bin/bash

output=~/$(date +%F_%H%M%S).png
temp_screenshot=$(mktemp).png

read -p "Move cursor to center and press Enter"
eval $(xdotool getmouselocation --shell)
x_center=$X
y_center=$Y
read -p "Move cursor to edge and press Enter"
eval $(xdotool getmouselocation --shell)

gnome-screenshot -f $temp_screenshot

radius=$(bc <<<"sqrt(($X-$x_center)^2+($Y-$y_center)^2)")

convert $temp_screenshot -alpha on \( +clone -channel a -evaluate multiply 0 -draw "ellipse $x_center,$y_center $radius,$radius 0,360" \) -compose DstIn -composite -trim "$output"

Save it as e.g. ~/circular_screenshot.bash and make it executable with chmod +x ~/circular_screenshot.bash. When you run it, the script first asks you to move the mouse cursor to the center position and press Enter and then to move it to an edge position (doesn’t matter which, the script calculates the radius from the distance) and again press Enter. The screen then flickers while the screenshot is taken (I recommend using scrot $temp_screenshot instead, it doesn’t show this odd behaviour.) and ImageMagick’s convert1 is used to crop the image. The output is saved with a timestamp as the filename in your home directory, you can change this behaviour by editing the output variable of the script.

Example output

example output

Call without (or better: with an invisible) terminal window

I suppose you don’t want to have a terminal blocking your screen every time you make a screenshot like that, so here’s a solution for that; Call the script as follows (assuming the script was saved as ~/circular_screenshot.bash):

xterm -geometry 0x0-1-1 -e ~/circular_screenshot.bash

This runs the script in an invisible terminal window (icon with a red “X” and a blue “T”), you just need to make sure it’s focused when you type Enter. You can assign this command to a keyboard shortcut using your desktop environment’s settings.

As ImageMagick is incredibly powerful you can adapt this script to output other shapes as well, I used ellipse with the same radius in both x and y direction to draw the circle above – change that to e.g. $radius,$(bc <<<"$radius*0.5") to get an ellipse with eccentricity above 0 instead.

1: I took the approach from this thread on imagemagick.org.