Bash脚本截取并保存图像 – Ubuntu

我写了一个bash脚本如下:

#!/bin/bash echo "Enter a file name" read filename import -window root $HOME/Desktop/$filename.png 

我希望这个脚本能够拍摄一个屏幕截图并在我的桌面上保存带有给定文件名的图像。

问题:我的Ubuntu终端出错:

导入:找不到命令

您需要安装包imagemagic才能使用import命令。

 sudo apt-get install imagemagick 

通过软件中心安装 ImageMagick的 安装imagemagick

这将允许您使用import命令。 还可以尝试使用graphicsmagick-imagemagick-compat包。

其他import替代品是

  • scrot

    安装scrot: sudo aptitude install scrot

通过软件中心安装 scrot 安装scrot

 usage: `scrot screen.png` 
  • gnome-screenshot

sudo aptitude安装gnome-screenshot

通过软件中心安装 GNOME的屏幕截图 安装gnome-screenshot

你可以使用最好的截图工具快门 安装快门 也用于脚本! 我相信,这是最强大的工具,并为您提供在脚本中使用的最先进和微调选项。

使用命令在终端中安装它

 sudo apt-get install shutter 

或使用软件中心(点击下方的大按钮)

通过软件中心安装

安装完成后,使用此命令获取整个显示的屏幕截图,并将屏幕截图保存在名为myshot.png的文件中

 shutter -f -o myshot.png -e 
  • -f告诉快门截取整个显示屏的截图。 您还可以使用-a截取活动窗口的屏幕截图,或者告诉它从任何特定窗口拍摄。

  • -o选项用于告知快门输出文件名。 您可以指定任何文件名

  • 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 (eg 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 (eg window title) $nb_name = like $name but without blanks in resulting strings $profile = name of current profile $R = random char (eg $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, eg 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. 

要安装包含程序import imagemagick,请参阅其他答案。 但是,您也可以通过调用gnome-screenshot从命令行启动gnome-screenshot 。 小程序将立即截取屏幕截图并显示一个对话框以输入文件名:

在此处输入图像描述