如何使用终端命令进行截屏?

在终端中,有哪些命令可以替代键盘上的“PrtSc”(打印屏幕)按钮?
我正在运行Ubuntu GNOME操作系统。

你是用gnome还是KDE还是其他什么?这取决于你所使用的桌面环境。 - Braiam
@Braiam 你看一下他的回答 - Avinash Raj
@AvinashRaj 你知道Unity也使用gnome-screenshot吗? - Braiam
1我认为gnome-screenshot是Unity上默认的截图工具。 - Avinash Raj
1可能是什么截图工具可用?的重复问题。 - Ciro Santilli OurBigBook.com
关于截屏的终端命令选项的问题。 - devav2
9个回答

你可以使用ImageMagick软件包中提供的import工具(如果系统上没有安装该软件包,您需要先安装它)。
然后在命令行中运行以下命令:
import screenshot.png

选择您想要捕捉的窗口,或者按住鼠标左键并拖动以选择一个区域。 import 实际上是一个非常强大的命令,可以用多种方式来捕捉屏幕。例如,要在延迟一段时间后捕捉整个屏幕并调整大小,请使用以下命令:
import -window root -resize 400x300 -delay 200 screenshot.png

要查看使用import命令的所有可用选项,请访问ImageMagick 网站
另一种从终端获取屏幕截图的方法是使用scrot
要安装scrot,请运行以下命令:
sudo apt-get install scrot

在Linux终端中使用scrot命令进行截屏。
scrot MyScreenshot.png

一些使用 scrot 的更多选项在这里:
scrot -b -d 5 '%Y:%m:%d:%H:%M:%S.png' -e 'mv $f ~/Desktop/'

在这个例子中:
--b指定截图应包括窗口边框。 --d指定延迟五秒。 -'%Y:%m:%d:%H:%M:%S.png'将以基于当前日期和时间的指定格式保存截图,本例中为.png。 --e 'mv $f ~/Desktop/'告诉scrot将截图保存在桌面上。

两个答案似乎只适用于当前的 $DISPLAY,因此在 SSH 上无法工作。 - That Brazilian Guy
这对许多用户来说肯定有效,只是对我没用。ImageMagickimport命令会将KDE桌面效果中的透明窗口边框/发光效果渲染为纯黑色。而scrot根本不愿意参与管道操作,而我正是使用管道操作来避免在从Xinerama设置中裁剪所需屏幕时产生不必要的中间文件。我改用xwd命令,并使用ImageMagickconvert命令将xwd格式转换为PNG并进行裁剪。(请参阅下面的回答) - Christian Hujer
1@那个巴西小伙子 每个图形命令都使用DISPLAY环境变量来确定连接哪个X服务器。如果在ssh上运行,只需在执行命令之前export DISPLAY=:0(或适当的值)。 - Jack

按下Ctrl+Alt+T打开一个终端,并运行以下命令:
gnome-screenshot

使用gnome-screenshot -d xx来延迟操作。例如,将截屏操作延迟10秒:
gnome-screenshot -d 10

或者

sleep 10;gnome-screenshot

屏幕截图保存在~/Pictures目录下。

7gnome-screenshot --help 会给你很多选项。 - somethis
7gnome-screenshot --interactive - Luis Souza

你可以使用shutter程序从终端中截取屏幕截图。在终端中运行以下命令来安装shutter,
sudo add-apt-repository ppa:shutter/ppa
sudo apt-get update
sudo apt-get install shutter

要截取活动窗口的屏幕截图,

shutter -a -o shot.png -e

要截取整个屏幕的截图,
shutter -f -o shot.png -e

截图保存在主目录中。
要获取更多选项,请运行shutter --help命令。
Usage:
    shutter [options]

Options:
    Example 1
            shutter -a -p=myprofile --min_at_startup

    Example 2
            shutter -s=100,100,300,300 -e

    Example 3
            shutter --window=.*firefox.*

    Example 4
            shutter --web=http://shutter-project.org/ -e

  Capture Mode Options:
    -s, --select=[X,Y,WIDTH,HEIGHT]
            Capture an area of the screen. Providing X,Y,WIDTH,HEIGHT is
            optional.

    -f, --full
            Capture the entire screen.

    -w, --window=[NAME_PATTERN]
            Select a window to capture. Providing a NAME_PATTERN (Perl-style
            regex) ist optional.

    -a, --active
            Capture the current active window.

    --section
            Capture a section. You will be able to select any child window
            by moving the mouse over it.

    -m, --menu
            Capture a menu.

    -t, --tooltip
            Capture a tooltip.

    --web=[URL]
            Capture a webpage. Providing an URL ist optional.

    -r, --redo
            Redo last screenshot.

  Settings Options:
    -p, --profile=NAME
            Load a specific profile on startup.

    -o, --output=FILENAME
            Specify a filename to save the screenshot to (overwrites any
            profile-related setting).

            Supported image formats: You can save to any popular image
            format (e.g. jpeg, png, gif, bmp). Additionally it is possible
            to save to pdf, ps or svg.

            Please note: There are several wildcards available, like

             %Y = year
             %m = month
             %d = day
             %T = time
             $w = width
             $h = height
             $name = multi-purpose (e.g. window title)
             $nb_name = like $name but without blanks in resulting strings
             $profile = name of current profile
             $R = random char (e.g. $RRRR = ag4r)
             %NN = counter

            The string is interpretted by strftime. See "man strftime" for
            more examples.

            As an example: shutter -f -e -o './%y-%m-%d_$w_$h.png' would
            create a file named '11-10-28_1280_800.png' in the current
            directory.

  Application Options:
    -h, --help
            Prints a brief help message and exits.

    -v, --version
            Prints version information.

    -d, --debug
            Prints a lot of debugging information to STDOUT.

    --clear_cache
            Clears cache, e.g. installed plugins, at startup.

    --min_at_startup
            Starts Shutter minimized to tray.

    --disable_systray
            Disables systray icon.

    -e, --exit_after_capture
            Exit after the first capture has been made. This is useful when
            using Shutter in scripts.

2Shutter是一个非常先进的工具,它具有像GIMP一样的编辑截图功能,并且可以轻松添加自动增量形状,非常适用于教程。 - guneysus

如果你想从登录终端(使用Ctrl+Alt+F1打开的那个)截取屏幕截图,你可以使用程序fbgrab
你可以通过输入sudo apt-get install fbcat来安装它。
然后,在你的登录终端中进行屏幕截图,输入以下内容:
$ sudo fbgrab my_screenshot

my_screenshot保存在当前目录下。


2my_screenshot的格式是什么?如何查看它? - Ciro Santilli OurBigBook.com
虽然在图形用户界面中有许多方法可以截屏,但这种方法实际上可以在纯控制台中工作,而无需X或任何图形用户界面。截屏的格式为PNG。 - mivk
适用于Ubuntu 18.04.5服务器,这是32位版本的最后一个发布版。该操作系统可以选择安装无需X,并且fbcat也不需要它。非常适合编写教程,其中tty终端是与服务器直接通信的唯一方法,并且需要截图。对此给予赞赏。 - Scooby-2
谢谢。我特意寻找这个答案,因为它不需要运行X服务器,并且在通过串口连接到一个盒子时可以让我截屏(用于调试和编写教程)。值得注意的是,该工具在“真实的终端”(例如通过SSH、终端访问等)中运行良好(在Ubuntu 20.04.2 LTS x86_64上),但如果在某些xterms或Guake中运行,则输出会损坏。 - Cloud

我尝试使用ImageMagick的import命令,但在使用KDE桌面特效时无法正常工作。ImageMagick的import命令会将窗口边框输出为黑色而不是正确地合并前景透明度和背景。
我还尝试使用X11的xwd命令和NetPBM的xwdtopnm命令,但对我来说也无法正常工作,因为NetPBM的xwdtopnm命令无法正确处理xwd命令的多屏幕输出,因为我有一个Xinerama设置。
但是,将X11的xwd命令与ImageMagick的convert命令结合使用对我来说完全没有问题:
xwd -silent -root | convert xwd:- screenshot.png

或者,如果你像我一样有一个双全高清Xinerama设置,并且只想要第一个屏幕:
xwd -silent -root | convert xwd:- -crop 1920x1080+0+0 test.png

仅针对第二屏幕:
xwd -silent -root | convert xwd:- -crop 1920x1080+1920+0 +repage test.png

不错的回答。但是由于你从pipe读取,你可能需要为转换命令指定输入格式:xwd ... | convert xwd:- ...。至少在我的Debian Strech安装中,使用IM 6.9.6-2时是必需的。 - Sylvain Leroux

我正在使用Ubuntu 13.10,并且我刚刚写了一个可能有帮助的脚本。我看到这个问题已经被回答过了,但是我的解决方案不需要额外安装。
#!/bin/bash
curDate=$(date)
imgExtension=".png"
imgName=$curDate$imgExtension
imgDirectory="/path/to/desires/save/directory/"
imgSavePath=$imgDirectory$imgName
gnome-screenshot --file="$imgSavePath"

这段代码将在不弹出对话窗口的情况下保存屏幕截图。它使用当前时间作为文件名,以避免重复文件名的问题。

无需安装。根据您的需求定制。
将以下命令放入一个名为 screenshot.bash 的文件中,放在任何你想放的地方。
gnome-screenshot -a -c -f /home//Desktop/Screenshot_$(date +"%0y%0m%0d_%0H%0M%0S").png
复制此文件的位置。
进入"键盘快捷键",通过在列表末尾按下+来创建一个新的快捷键。 enter image description here

在Linux上,您还可以使用美味的flameshot
它不仅在交互式工作中表现良好,而且还可以作为命令行的朋友为您提供服务。
想象一下,您想要屏幕的一个精确部分:
1. 启动终端 2. 输入flameshot gui -g以获取区域坐标 3. 验证截图,可能是CTRL+C 4. 然后一次完成所有操作,无需交互:flameshot gui --region 1771x1019+1940+120 -s -p pipo.png 说明:
  • gui 启动图形界面模式
  • --region 裁剪在第2步中从-g获得的屏幕区域
  • -s 不等待用户验证,自动保存
  • -p 文件或文件夹的路径以保存。⚠️ 它不会覆盖文件名,但你会得到一个带有后缀的pipo_1.png(以此类推)。生成的文件名将打印在stderr

玩得开心。


如果你只需要通过ssh偷窥远程计算机,你可以使用这个脚本。
#!/bin/bash
computer='JohnBrownsBody@10.0.0.11'
password='LiesAMoulderingInTheGrave'
path='/tmp/'
file='peepshow.png'

[ "$(whereis sshpass | cut -d: -f2)" == "" ] && sudo apt install sshpass
[ "$(whereis feh | cut -d: -f2)" == "" ] && sudo apt install feh

sshpass -p "$password" ssh $computer "export DISPLAY=:0; scrot $path$file;" 
sshpass -p "$password" scp -r "$computer:$path$file" $path
# gpicview "$path$file"
feh "$path$file"
echo "... and his soul is marching on in $path$file! Peek at it!"