从命令行中截取特定区域的屏幕截图

我正在使用Lubuntu桌面12,并且正在寻找一个可以从命令行中截取特定区域的工具。
我已经尝试了Shutter。它能够正常工作,但是当我运行命令时会生成警告(Wnck-WARNING **: Unhandled action type)。我认为这个工具可能是设计用于GNOME环境,可能与Lubuntu不兼容。截图成功,但命令却挂起,这对我来说无法接受。
那么有没有一个好的截图工具满足以下要求:
  1. 可以从命令行运行
  2. 可以捕捉桌面的特定区域

我想补充一下,Lubuntu附带的工具scrot没有裁剪特定坐标的选项,只能交互式地定义一个区域,这不是我想要的。

1你试过使用shutter--exit_after_capture选项吗?此外,许多图形界面程序在命令行运行时会生成这样的警告。真的没什么好担心的。 - muru
谢谢,我阅读了man页面,但没有找到这个开关,尽管命令在捕获后需要一些时间才能完成,但它确实可以。因此,我的脚本可以继续执行。 - AL-Kateb
6个回答

在Lubuntu中,你可以做任何你想做的事情:通过命令行使用以下命令来截取屏幕截图:
scrot_extended 100 100 400 400

使用下面的脚本。

四个参数是 <x>, <y>, <width>, <height>
我还没有在 Lubuntu 12.04 中测试过它,但似乎不太可能不起作用;它使用的是 python 2 和基本的命令行工具,这些工具已经存在很长时间了。

解释

这个脚本:

  • 使用 scrot 进行截屏
  • 将截屏保存到临时文件中
  • 使用 imagemagick 创建一个新的图像,根据你运行脚本时的参数进行裁剪
  • 将图像以编号的文件形式保存到目录中,以防止覆盖

如何使用

  1. 脚本同时使用了scrotimagemagick。确保你的系统上已经安装了scrot。要安装imagemagick,请执行以下命令:

     sudo apt-get install imagemagick
    
  2. 将脚本复制到一个空文件中。

  3. 默认情况下,图像会保存在~/scrot_images目录下,并以outputfile_1.pngoutputfile_2.png等命名。如果需要更改保存路径,请按照脚本中的标记进行修改。注意,如果更改了目录,必须使用完整路径

  4. 将文件保存到~/bin目录中(如果需要,创建该目录),并将其命名为scrot_extended(无扩展名),然后赋予可执行权限

  5. 退出当前账户并重新登录,然后使用以下命令进行截屏:

     scrot_extended <x> <y> <width> <height>
    

例子:

scrot_extended 100 100 400 400

enter image description here

输出文件:

enter image description here

剧本

#!/usr/bin/env python
import subprocess
import os
import sys
# setting default directories / filenames
home = os.environ["HOME"]
temp = home+"/"+".scrot_images"
img_in = temp+"/in.png"
# if you prefer, you can change the two line below:
output_directory = home+"/"+"scrot_images" # output directory
filename = "outputfile"                    # filename
# creating needed directories
for dr in [temp, output_directory]:
    if not os.path.exists(dr):
        os.mkdir(dr)
# creating filename (-number) to prevent overwriting previous shots
n = 1
while True:
    img_out = output_directory+"/"+filename+"_"+str(n)+".png"
    if os.path.exists(img_out):
        n = n+1
    else:
        break
# reading arguments,arranging commands to perform
coords = sys.argv[1:5]
cmd1 = ["scrot", img_in]
cmd2 = ["convert", img_in, "-crop", coords[2]+"x"+coords[3]+"+"+coords[0]+"+"+coords[1], "+repage", img_out]
# Take screnshot, crop image
for cmd in [cmd1, cmd2]:
    subprocess.call(cmd)

非常感谢您的详细回答,我不确定其他工具是否像Shutter一样具有内置功能。它们是先拍摄整个屏幕然后裁剪吗?我认为不会。因为您知道这种方式比拍摄整个桌面截图再裁剪更快速和高效。无论如何,这个脚本都非常方便,再次感谢。 - AL-Kateb
@AL-Kateb 我很高兴它能正常工作!虽然我不确定,但我猜其他工具可能会先截取整个屏幕然后进行处理,因为Shutter在我的系统上完成相同任务所需的时间要(多得)多。 - Jacob Vlijm
1@JacobVlijm,如果始终要捕获相同的区域,那么类似于scrot /tmp/img-in.png -e "convert /tmp/img-in.png -crop 100X200+300+400 ~/Desktop/"$(date +%Y%m%d%H%M%S)".png"这样的命令就足够了吗? - DK Bose

使用maim比使用scrot_extended的接受答案更容易。现在关于如何在maim中获取固定大小截图的回答已经过时。

maim允许您使用geometry(g)标志指定几何形状。如文档所述:

 -g, --geometry=GEOMETRY
              Sets the region to capture, uses local coordinates from the given
              window. So -g 10x30-5+0 would represent the rectangle wxh+x+y where
              w=10, h=30, x=-5, and y=0. x and y are the upper left location of this rectangle.

使用方法:

maim -g <w>x<h>+<x>+<y> <output-file>

例子:

maim -g 1024x1024+10+20 ~/test.png

2您可以使用slop来交互式地获取这些几何坐标 - Anthon

使用 maim

概述

maim(make image)是一个新的截图工具,旨在作为scrot的改进版本。

maim带来的众多新功能之一是支持从命令行界面设置屏幕捕获区域。语法如下:

maim -x <x-coordinate> -y <y-coordinate> -w <width> -h <height>

e.g.:

maim -x 100 -y 100 -w 400 -h 400

安装

maim 还没有出现在官方软件仓库中,也不是任何 PPA 的一部分。您需要从源代码编译然后安装。

确保所有依赖项已满足后...

sudo apt-get install build-essential cmake
sudo apt-get install libimlib2-dev libxrandr-dev libxfixes-dev

...我们可以继续进行实际的编译和安装:
git clone https://github.com/naelstrof/maim.git
cd maim
cmake ./
make && sudo make install

就是这样。现在你应该能够从终端中调用maim了。请确保查看文档(maim --help)以获取所有可用选项;还可以查看slop,这是同一开发者的一个实用工具,可以让你交互式地选择屏幕截图的区域。

1这似乎已经不再是最新的了。maim的5.4版本没有你所描述的命令行参数-x、-y、-w和-h与该功能相关。相反,必须使用--geometry选项。 - josch
改变屏幕分辨率后,Maim似乎变得非常不稳定。 - marinara

当前版本的scrot具有截取屏幕上定义矩形的能力。

scrot --autoselect '1,2,3,4' outfile.png # 在1,2处截取宽度为3,高度为4的矩形

当然,您需要将1,2替换为您想要的矩形的x、y像素坐标。因此,x、y将是您所需矩形的左上角像素。

还可以参考this answer以了解如何从bash脚本中获取当前鼠标坐标的说明。


将实现scrot和imagemagick的bash脚本绑定到键盘快捷键

这个脚本与Jacob Vlijm的答案几乎相同,但使用的是bash。它使用时间戳命名文件,以避免覆盖现有文件。它还允许您在脚本中定义默认的裁剪参数,因此您不需要使用任何参数来调用它。

按照以下方式调用下面的脚本(假设您在存储脚本的目录中,否则需要提供脚本的完整路径):

  • 带参数:./screenshot.sh $left_px $top_px $width_px $height_px 或者
  • 不带参数:./screenshot.sh,此时将使用脚本中指定的默认参数。

1)安装必要的应用程序

从命令行运行:

sudo apt install scrot imagemagick

2)创建脚本
打开您选择的文本编辑器,并创建一个包含以下内容的新纯文本文件。确保在顶部修改变量,以指定您想要保存图像的位置以及要裁剪出屏幕的哪个部分。参见this trick以获取可以用于查找lefttop并计算widthheight的鼠标坐标。
#!/bin/bash

# Change these values to match your preferences
imageQuality=100    # scrot default is 75
screenshotDir="/tmp"    # directory in which to save screenshots
imageName="$(date +%Y-%m-%d.%H:%M:%S.%N).jpg"   # save image names as timestamp
default_left=10     # begin crop this number of pixels from the left of the image
default_top=10      # begin crop this number of pixels from the top of the image 
default_width=100   # crop this many pixels wide
default_height=100  # crop this many pixels tall

#Do not make any more changes from here down unless you know what you're doing
l=$1; t=$2; w=$3; h=$4
left=${l:=$default_left}
top=${t:=$default_top}
width=${w:=$default_width}
height=${h:=$default_height}
imagePath="$screenshotDir/$imageName"

[ ! -d "$screenshotDir" ] && mkdir -p "$screenshotDir"
scrot -q $imageQuality "$imagePath"
convert "$imagePath" -crop ${width}x${height}+${left}+${top} "$imagePath"

将此脚本保存在您喜欢的任何位置,并使其可执行。假设您将脚本命名为screenshot.sh,您可以在命令行中执行以下操作: chmod +x /path/to/your/script/screenshot.sh 3) 将此脚本绑定到键盘快捷键 (可选) 按照此处找到的说明创建自定义键盘快捷键。当您需要输入命令时,请输入完整路径到您的screenshot.sh文件(包括文件名)。