我需要一个能够执行以下操作的工具:选择一个窗口,将使用 x 填充制作该窗口的屏幕截图,如下图所示:
因此,在大多数情况下, x 将与 y 相等,但有时我需要不同的距离。
如何自动制作这样的截图?我尝试过Shutter,但我找不到那样的设置。但是,它支持插件。所以插件可能就是以这种方式裁剪窗口。
我认为它不存在,但就像任何东西一样,它可以制作。
如果您使用以下组合键提供下面的脚本(下面进一步说明),则会弹出一个窗口,允许您在左,右,顶部上设置屏幕截图的边距 bottom ,用空格分隔:
结果:
或:
结果:
等
我将默认值设置为30像素,但您可以设置任何默认值(见下文)。
该脚本使用Shutter
和wmctrl
。假设Shutter
已经在你的系统上(因为你提到它),安装wmctrl
:
sudo apt-get install wmctrl
N.B。如果您使用 Kubuntu ,默认情况下不会安装Zenity
:
sudo apt-get install zenity
将以下脚本复制到空文件中。如果您愿意,可以在脚本行中更改“默认”marge:
`arg =`
将其另存为custom_screenshot.py
。
将脚本添加到密钥快捷方式组合中:选择:系统设置> “键盘”> “快捷方式”> “自定义快捷方式”。单击“+”并添加命令:
python3 /path/to/custom_screenshot.py
该脚本使用wmctrl
来确定窗口的位置。但是,在不同的窗口管理器上,wmctrl -lG
命令的输出显示窗口y位置的微小差异。这些差异通过在脚本的deviation=
-line中设置的值消除。当前设置的值(0)适用于Unity和KDE。
该脚本也经过测试,可以在Xfce
和Gnome
上正常工作,但是需要更改该值,如脚本的head部分所述。
#!/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])