17

I need a tool that will do the following: selecting a window, will make a screenshot of that window with a x padding, like in the following image:

So, in most of the cases x will be equal with y, but sometimes I need different distances.

How to make such a screenshot, automatically? I tried with Shutter, but I couldn't find such a setting there. However, it supports plugins. So a plugin could be to crop the window this way.

  • Can I ask what tool you used to create that mockup? It looks quite nice :) – Seth Feb 8 '15 at 18:16
  • 1
    @Seth InkScape rules! I'm not an expert, but I do like creating stuff like this for my projects. It looks good for eyes. :-) Here is the PNG version, and the SVG one. This repository contains other stuff created with InkScape. – Ionică Bizău Feb 8 '15 at 19:52
19

Script, using Shutter

I don't think it exists, but like anything, it can be made.

If you make the script below available under a key combination (explanation further below), a window will pop up, allowing you to set the margins of your screenshot on the left, right, top and bottom, separated by a space:

enter image description here

result:

enter image description here

or:

enter image description here

result:

enter image description here

etc.

I set the default to 30 px, but you can set any default value (see below).

How to use

  • The script uses Shutter and wmctrl. Assuming Shutter is already on your system (since you mentioned it), install wmctrl:

    sudo apt-get install wmctrl
    

    N.B. If you use Kubuntu, Zenity is not installed by default:

    sudo apt-get install zenity
    
  • Copy the script below into an empty file. If you want you can change the "default' marge in the line of the script:

    `arg =`
    
  • save it as custom_screenshot.py.

  • Add the script to a key shortcut combination: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

    python3 /path/to/custom_screenshot.py
    

Note

The script uses wmctrl to determine the position of the window. On different window managers however, the output of the wmctrl -lG command shows small differences in the y-position of the window. These differences are eliminated by the value, set in the deviation= -line of the script. The currently set value (0) is appropriate for Unity and KDE.

The script is also tested, and works fine on Xfce and Gnome, but the value needs to be changed then, as explained in the head section of the script.

The script

#!/usr/bin/env python3
import subprocess
import time

"""
On different window managers, the window geometry as output of wmctrl differs slightly.
The "deviation" should compensate these differences. Most likely appropriate (tested) settings:
Unity: 0, Gnome: -36, Xfce (Xubuntu): -26, KDE (Kubuntu): 0
"""
#---
deviation = 0
#---
get = lambda cmd: subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")
try:
    arg = get('zenity --entry --entry-text "30 30 30 30" --text "border left, right, top, bottom" --title "Screenshot margins"').strip().split()
except:
    pass
else:
    time.sleep(0.5)
    # frontmost window pos
    frontmost = [l.split()[4] for l in get("xprop -root").splitlines() if "ACTIVE_WINDOW(WINDOW)" in l][0].replace(",", "")
    frontmost = frontmost[:2]+"0"+frontmost[2:]
    f_data = [l.split() for l in get("wmctrl -lG").splitlines() if frontmost in l][0][2:6]
    # extent
    xt_data = get("xprop -id "+frontmost).split()
    xt_i = xt_data.index("_NET_FRAME_EXTENTS(CARDINAL)")
    xt = [int(n.replace(",", "")) for n in xt_data[xt_i+2:xt_i+6]]
    # set data for screenshot command
    x = str(int(f_data[0])-int(arg[0])-xt[0])
    y = str(int(f_data[1])-int(arg[2])-xt[2]+deviation)
    w = str(int(f_data[2])+int(arg[0])+int(arg[1])+xt[0]+xt[1])
    h = str(int(f_data[3])+int(arg[3])+int(arg[2])+xt[2]+xt[3])

    command = "shutter -s="+(",").join([x,y,w,h])+" -e"
    subprocess.call(["/bin/bash", "-c", command])
  • 3
    I don't think it exists -- Now it exists! Thank you a lot! – Ionică Bizău Jan 28 '15 at 11:09
  • 1
    @IonicăBizău you're welcome! Thanks for the question, it was fun to do. I made a small correction on the default setting explanation. – Jacob Vlijm Jan 28 '15 at 11:11
  • 1
    @IonicăBizău found (and fixed) a bug in geometry. – Jacob Vlijm Jan 28 '15 at 13:14
  • 2
    Nice, nice, nice! I set it as the comand that I run when I press the special button on my ASUS notebook. :-) – Ionică Bizău Jan 28 '15 at 13:17
2

You can also use combination of Shift and PrtScr buttons to take screen-shot of a specific area with user-defined dimensions.

Just press the combination and use the modified-cursor ( it becomes similar to a plus sign ) to choose the area for screen-shot.

  • Usually, I cannot control my hands to set these margins exactly. :-) But +1 for this shortcut, I didn't know it. – Ionică Bizău Feb 3 '15 at 15:46
2

You can take screenshots with the scrot command line screen capture utility:

scrot -s  

or

scrot -ub -d 5  

The second command puts a border around the selected window, which has a nice width in relation to the size of the window. The -d 5 option stands for delay and gives you a delay of 5 seconds to select the window to be in the screenshot.

Install with this:

sudo apt-get install scrot

Reference: Ubuntu Manual - scrot

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.