如何从窗口截取屏幕截图,并具有可自定义的边距

我需要一个工具来执行以下操作:选择一个窗口,将使用x填充制作该窗口的屏幕截图,如下图所示:

因此,在大多数情况下, x将与y相等,但有时我需要不同的距离。

如何自动制作这样的截图? 我尝试过Shutter,但我找不到那样的设置。 但是,它支持插件。 所以一个插件可能就是以这种方式裁剪窗口。

脚本,使用快门

我不认为它存在,但像任何东西一样,它可以制作。

如果您使用以下组合键提供下面的脚本(下面进一步说明),则会弹出一个窗口,允许您在左侧,右侧,顶部底部设置屏幕截图的边距,用空格分隔:

在此处输入图像描述

结果:

在此处输入图像描述

要么:

在此处输入图像描述

结果:

在此处输入图像描述

等等

我将默认值设置为30像素,但您可以设置任何默认值(见下文)。

如何使用

  • 该脚本使用Shutterwmctrl 。 假设Shutter已经在你的系统上(因为你提到它),安装wmctrl

     sudo apt-get install wmctrl 

    注意如果您使用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。

该脚本也经过测试,在XfceGnome上运行正常,但是需要更改值,如脚本的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]) 

您还可以使用ShiftPrtScr按钮的组合来拍摄具有用户定义尺寸的特定区域的屏幕截图。

只需按下组合并使用修改的光标(它变得类似于加号)来选择屏幕截图的区域。

您可以使用scrot命令行屏幕捕获实用程序scrot屏幕截图:

 scrot -s 

要么

 scrot -ub -d 5 

第二个命令在所选窗口周围放置一个边框,该窗口具有相对于窗口大小的宽度。 -d 5选项代表延迟,并为您提供5秒的延迟,以选择屏幕截图中的窗口。

用这个安装:

 sudo apt-get install scrot 

参考: Ubuntu手册 – scrot