Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Takes a screenshot of a predefined area of the desktop
#!/bin/bash
# Takes a screenshot of a predefined area of your desktop
# Good for comparison images and making animations where FPS is too low for OBS
#
# Dependencies:
# - scrot
# - graphicsmagick
# Change these values to match your preferences
imageQuality=100 # scrot default is 75
screenshotDir="." # directory in which to save screenshots
# Capture area
# -> main screen without title bar and task bar = 1920, 37, 2560, 1330
default_left=1920 # begin crop this number of pixels from the left of the image
default_top=37 # begin crop this number of pixels from the top of the image
default_width=2560 # crop this many pixels wide
default_height=1330 # crop this many pixels tall
# Allow override of capture rectangle specified from the command line
l=$1; t=$2; w=$3; h=$4
left=${l:=$default_left}
top=${t:=$default_top}
width=${w:=$default_width}
height=${h:=$default_height}
# Look for next free filename in sequence
i=1
imagePath=$screenshotDir/screenshot-$(printf "%04d" $i).png
while [[ -e $screenshotDir/screenshot-$(printf "%04d" $i).png ]] ; do
let i++
done
imagePath=$screenshotDir/screenshot-$(printf "%04d" $i).png
# Do the screenshottery
# Crop will capture everything, so we'll export to .bmp (fast loading/saving),
# then use GraphicsMagick to crop and convert to .png
[ ! -d "$screenshotDir" ] && mkdir -p "$screenshotDir"
scrot -q $imageQuality "/tmp/screenshot.bmp"
gm convert "/tmp/screenshot.bmp" -crop ${width}x${height}+${left}+${top} "$imagePath"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment