从命令行截取特定区域的屏幕截图
我正在运行Lubuntu桌面12,我正在寻找一个可以从命令行截取某个区域的屏幕截图的工具。
我试过快门。 它可以工作但是,当我运行命令时它会生成警告( Wnck-WARNING **: Unhandled action type
)。 我认为该工具可能设计为在GNOME下运行,可能与Lubuntu不兼容。 屏幕截图已成功拍摄,但命令挂起,这是我无法使用的。
那么什么是一个很好的截图工具呢
- 从命令行运行
- 可以捕获桌面的某个区域
我想补充一点,Lubuntu附带的工具scrot
,没有裁剪某些坐标的选项,但只有交互式的用户定义区域,这不是我想要的。
在Lubuntu中,您可以完全按照自己的意愿执行操作:使用命令从命令行进行屏幕截图:
scrot_extended 100 100 400 400
使用下面的脚本。
四个参数是
。
我没有机会(还)在Lubuntu 12.04
测试它,但它似乎不太可能不起作用; 它使用python 2
和已经存在了很长时间的基本命令行工具。
说明
剧本:
- 用
scrot
截取屏幕截图 - 将其保存到临时文件中
- 使用
imagemagick
,它会使用您运行脚本的参数创建一个新图像,裁剪屏幕截图 - 图像作为编号文件保存到目录中,以防止覆盖
如何使用
-
该脚本使用
scrot
和imagemagick
。scrot
应该在你的系统上。 要安装imagemagick:sudo apt-get install imagemagick
-
将脚本复制到空文件中
- 默认情况下,图像保存到
~/scrot_images
,名为:outputfile_1.png
,outputfile_2.png
等。 如果需要,可以更改它,如脚本中标记的那样。 请注意,如果更改了diretory,则必须使用完整路径 。 - 将文件保存到
~/bin
(如果需要,创建目录)作为scrot_extended
(无扩展名)并使其可执行 。 -
注销并重新登录并使用以下命令截取屏幕截图:
scrot_extended
例:
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)
使用maim
概观
maim
(make image)是一个新的截图实用程序,它被设计为scrot
的改进版本。
maim
附带的许多新function之一是支持从CLI设置屏幕捕获区域。 语法如下:
maim -x -y -w -h
例如:
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 --help
); 并查看slop
,由同一个开发人员提供的实用工具,允许您以交互方式选择屏幕截图的区域。
将实现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)创建脚本
打开您选择的文本编辑器并创建一个包含以下内容的新纯文本文件。 确保修改顶部的变量,以指定要保存图像的位置以及要裁剪的屏幕部分。 查看此技巧可获取鼠标坐标,可用于查找left
和top
以及计算width
和height
。
#!/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
文件(包括文件名)。
根据wiki的这篇文档: https: //wiki.ubuntu.com/Lubuntu/Applications/Process%20Documentation xfce4-screenshooter是该发行版的截图应用程序之一。 在我的XFCE设置中,以下命令执行您要求的操作。
xfce4-screenshooter