Clé*_*ent 5 command-line scripts screenshot image-processing
我使用屏幕截图作为 GUI 软件的回归测试。在部署每个新版本之前,都会针对新旧版本运行一系列自动任务,两种情况下每个命令后都会生成屏幕截图,并比较结果。ImageMagick 的导入命令在这方面运行良好。
最近我添加了右键菜单。不幸的是,import -window 'id'没有捕获这些菜单。
Ubuntu 上的哪些命令行工具可以截取窗口的屏幕截图,以及它上面的所有窗口?
也就是说,有哪些工具可以不截取一个窗口ID对应的窗口的截图,而是截取整个屏幕的截图并将其截断到给定窗口的边界?
我无法使用什么是截取屏幕截图的终端命令中列出的任何工具以简单的方式获得此结果?.
使用shutter和wmctrl,此脚本的编辑版本几乎完全符合您的描述:它会截取该区域的屏幕截图,屏幕上覆盖特定窗口,无论该窗口是否以及如何(部分)低于其他窗口。
的玛吉围绕窗,要被包括在所述屏幕截图,是任意的; 如果您愿意,请将其设置为零。
Inkscape屏幕上有一个窗口,带有 id 0x0520000e,部分被几个gedit窗口覆盖。我使用窗口 id 和px窗口周围的边距 (in ) 作为参数运行脚本:
python3 <script> 0x0520000e 10 10 10 10
Run Code Online (Sandbox Code Playgroud)
(左侧/右侧/顶部/底部窗口周围10 10 10 10的边距在哪里px。设置为0图像中没有边距)
结果:
python3 <script> 0x0520000e 10 10 10 10
Run Code Online (Sandbox Code Playgroud)
该脚本使用Shutter和wmctrl:
sudo apt-get install wmctrl shutter
Run Code Online (Sandbox Code Playgroud)将下面的脚本复制到一个空文件中,另存为custom_screenshot.py.
通过以下命令运行它:
python3 /path/to/custom_screenshot.py <window_id> <left> <right> <top> <bottom>
Run Code Online (Sandbox Code Playgroud)
其中 ,<left> <right> <top> <bottom>是您想要保留在窗口周围图像中的边距,就像在这个答案中一样。
示例命令:
python3 /path/to/custom_screenshot.py 0x0520000e 20 20 20 20
Run Code Online (Sandbox Code Playgroud)在 中Shutter,可以截取桌面定义区域的屏幕截图。
以窗口 id 作为参数,脚本在wmctrl(wmctrl -lG准确地说)的帮助下查找窗口的确切位置,以及xprop -id <window_id>(_NET_FRAME_EXTENTS(CARDINAL) = 0, 0, 28, 0例如在行中)的输出。
随后,从找到的区域截取屏幕截图,带有任意边距。
该脚本不会覆盖现有的屏幕截图。新截图命名为:
outputfile_1.png
outputfile_2.png
outputfile_3.png
Run Code Online (Sandbox Code Playgroud)
等等...
基于这个脚本,如果我们做同样的伎俩,但使用Scrot的替代Shutter,我们可以跳过sleep 0.5,使其很多速度快:
#!/usr/bin/env python3
import subprocess
import time
import sys
"""
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")
time.sleep(0.5)
# targeted window
target = sys.argv[1]; arg = sys.argv[2:]
f_data = [l.split() for l in get("wmctrl -lG").splitlines() if target in l][0][2:6]
xt_data = get("xprop -id "+target).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])
Run Code Online (Sandbox Code Playgroud)
像第一个脚本一样使用它,只是:
此脚本需要scrot,imagemagick和wmctrl
sudo apt-get install imagemagick wmctrl scrot
Run Code Online (Sandbox Code Playgroud)图像将存储在 ~/scrot_images
虽然第一个脚本使用命令行选项Shutter拍摄桌面的定义部分,Scrot但不支持。它只需要整个屏幕的截图。
然而,我们可以结合使用imagemagick的选项来制作图像,以及找到我们在第一个脚本中使用的确切窗口坐标的方法,并相应地裁剪图像。
由于Scrot它的重量极轻且速度快,甚至结合imagemagick's 裁剪动作,我们有一种非常快速的方法来制作窗口区域的屏幕截图。
还是不够快?
不确定是否需要它,但是通过一些重写(请参阅下面的脚本),可以通过先拍摄整个系列,然后(之后)进行裁剪来更快地制作一系列镜头。假设窗口将保持在其位置,这将节省相当多的时间:
仅拍摄Scrot(无裁剪):
real 0m0.263s
user 0m0.205s
sys 0m0.037s
Run Code Online (Sandbox Code Playgroud)拍摄,包括裁剪:
real 0m0.363s
user 0m0.293s
sys 0m0.040s
Run Code Online (Sandbox Code Playgroud)最后,作为制作一系列截图的例子,下面的脚本,如编辑中所建议的。
这个首先连续拍摄所有图像,然后一次对所有创建的图像进行裁剪。
使用类似于第二个脚本的脚本,但有一个额外的参数:连续拍摄的数量,例如:
python3 /path/to/custom_screenshot.py 0x0520000e 0 0 0 0 20
Run Code Online (Sandbox Code Playgroud)
0x0520000e连续制作 20 个窗口的屏幕截图(可能是数百个),窗口周围没有边缘。
sudo apt-get install wmctrl shutter
Run Code Online (Sandbox Code Playgroud)
import与选项一起使用-screen,例如
import -screen -window 'id' test.png
Run Code Online (Sandbox Code Playgroud)