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

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

我正在运行 Lubuntu 桌面 12,我正在寻找一个可以截取特定区域从命令行。

我尝试过 Shutter。它可以工作,但是当我运行命令时,它会生成警告 ( Wnck-WARNING **: Unhandled action type)。我认为该工具可能设计为在 GNOME 下运行,可能与 Lubuntu 不兼容。屏幕截图成功,但命令挂起,这是我无法处理的。

那么,有什么好的截图工具

  1. 从命令行运行
  2. 可以捕捉桌面的特定区域

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

答案1

在 Lubuntu 中,你可以做你想做的事:使用以下命令从命令行截取屏幕截图:

scrot_extended 100 100 400 400

使用下面的脚本。

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

解释

剧本:

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

如何使用

  1. 该脚本同时使用scrotimagemagickscrot应该在您的系统上。要安装 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

在此处输入图片描述

输出文件:

在此处输入图片描述

剧本

#!/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)

答案2

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

答案3

使用maim


概述

maim(制作图像)是一个新的截图实用程序,旨在作为的改进版本scrot

众多新功能之一maim是支持直接从 CLI 设置屏幕捕获区域。语法如下:

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

例如:

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,由同一开发人员开发的实用程序,可让您以交互方式选择屏幕截图的区域。

答案4

将实现 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)创建脚本

打开您选择的文本编辑器并创建一个包含以下内容的新纯文本文件。请务必修改顶部的变量以指定要保存图像的位置以及要裁剪屏幕的哪一部分。请参阅这个技巧用于获取鼠标坐标,可用于查找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文件的完整路径(包括文件名)。

相关内容