Open In App

Shell Script to Take a Screenshot

Last Updated : 25 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes we need to capture the screen for later. There are various ways to perform this task: one is achieved by using the shortcut key present in the system, and the other is by using some command-line tools. Here we are going to understand those command-line tools.

Different ways to take screenshots in Ubuntu

Before starting the command line tools, we must know about the various shortcuts that can produce different screen outputs. Knowing all of them will help you to create the desired output image.

  • PtrScr: PtrScr will capture the whole screen and save the image file to the Pictures folder in the Home directory.
  • Shift + Ctrl + PtrScr: This shortcut is to copy to the clipboard. Here, you can select the area to be copied.
  • Shift + Alt + PtrScr: This shortcut captures the active window and copies it to the clipboard.
  • Shift + PtrScr: This shortcut capture a specific area to the clipboard.
  • Alt + PtrScr: This shortcut will capture the active window and save it to Pictures.
  • Ctrl + PtrScr:  This shortcut will copy the whole screen to the clipboard.

Tools to Capture Screenshots in Ubuntu

Here we have some CLI tools which can help to capture the screen. They have different variations like capturing the whole screen, capturing the active window, deciding the screenshot’s size, choosing a specific area to capture, etc.

ImageMagick

ImageMagick can read, write images in various formats like PNG, JPEG, GIF, HEIC, TIFF, DPX, EXR, Webp, Postscripts, PDF, and SVG.

Various options present in ImageMagick

  • Resize
  • Flip
  • Mirror
  • Rotate
  • Distort
  • Shear
  • Draw text, lines, polygons, ellipses, etc.

Installation:

# sudo apt install imagemagick –y
Shell Script to Take a Screenshot

Install Imagemagick

Scrot:

Some features of scrot:-

  • Optimized for image quality.
  • Scrot supports multi-image formats like PNG,  JPEG,  JPG, GIF, etc.
  • Capture a specific window or rectangular area using a switch.

Installation:

# sudo apt install scrot
Shell Script to Take a Screenshot

Install scrot

Gnome-screenshot:

Gnome screenshot features:-

  • Capture the screen,
  • Or a window,
  • Or a specific area,
  • And save it to a file.

Installation:

# sudo apt install gnome-screenshot
Shell Script to Take a Screenshot

Install gnome-screenshot

Prepare a Shell Script to Capture a Screenshot

This shell script will take various screenshot properties and then produce the screenshot according to those constraints. Some of the conditions we are going to use are the name of the screenshot,  area of the screenshot, etc. The script will ask you to use any given tool and capture a specific region.

  • Press [ 1 ] for Capturing the whole Screen using gnome-screenshot
  • Press [ 2 ] for Capturing the Active window using the Scrot
  • Press [ 3 ] for Capturing the Active window using the gnome-screenshot
  • Press [ 4 ] for Capturing the Specific Area using the Scrot
  • Press [ 5 ] for Capturing the Specific Area using the gnome-screenshot

Open a file using gedit

# gedit screenshotApp.sh
#!/bin/bash
#This is a shell script to take Screenshots
echo -e "
[ 1 ] for Capturing the whole Screen using gnome-screenshot \n
[ 2 ] for Capturing the Active window using the Scrot \n
[ 3 ] for Capturing the Active window using the gnome-screenshot \n
[ 4 ] for Capturing the Specific Area using the Scrot \n
[ 5 ] for Capturing the Specific Area using the gnome-screenshot \n"

#Now read the user input
read UserInput

#Now we will use switch cases to implement the UserInput

case $UserInput in
    1)
    #command for Capturing the whole Screen using gnome-screenshot.
    gnome-screenshot
    # By default it will capture the whole screen
    ;;
    2)
    #command for Capturing the Active window using the Scrot.
    # syntax scrot <option> <optional filename>
    scrot -s
    # -s is to select the rectangular area or just click on the window you want to capture
    ;;
    3)
    #command for Capturing the Active window using the gnome-screenshot.
    # syntax gnome-screenshot <option>
    gnome-screenshot -w
    # here -w is for the active window
    ;;
    4)
    #command for Capturing the Specific Area using the Scrot.
    # syntax scrot <option> filename
    scrot -s myscreenshot.png
    # here -s is interactive window decision
    ;;
    5)
    #command for Capturing the Specific Area using the gnome-screenshot.
    gnome-screenshot -a
    # here -a is for specific area
    ;;
    *)
    echo -e "Please Enter Correct Input \n"
    ;;
esac

Give executable permissions

# chmod +x screenshotApp.sh

Run and give UserInput

# ./screenshotApp.sh

Shell Script to Take a Screenshot

Output Screenshot

Whole screen output

Run again and choose a different option this time

#./screenshotApp.sh

Shell Script to Take a Screenshot

The output screenshot

Selected area using mouse


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads