Shell脚本使用ImageMagick打开随机jpeg

时间:2019-06-07 12:11:01

标签: shell imagemagick

我有大量的jpeg。我想编写一个shell脚本,该脚本随机选择5张图像,然后使用imageMagick将它们放在蒙太奇中,然后打开此蒙太奇文件。我希望此过程每10秒发生一次。

我已经尝试过该脚本

for f in *.jpg
do
shuf -ezn 5 * | xards -0 -n1 | montage *.jpg | display montage.jpg
done

但不起作用

它将打开所有图像,并显示以下错误消息

image.sh 7: image.sh: Xards: not found 

go.sh脚本与图像保存在同一文件夹/目录中,现在看起来像这样:

#!/bin/bash 

# Get list of files into array - just once at start
files=(*.jpg)

# Do forever
first=0
while :; do
   # Shuffle array
   files=( $(shuf -e "${files[@]}") )

   # Make montage of first 5 images in shuffled array
   magick montage ${files[0]} ${files[1]} ${files[2]} ${files[3]} ${files[4]} montage.jpg

   # Start displaying if first pass - leaving "display" running in background updating itself every second
   if [ $first -eq 0 ] ; then
      display -update 1 montage.jpg &
      first=1
   fi

   # Snooze 10 and repeat
   sleep 10
done

但是返回

go.sh: 4: go.sh: Syntax error: "(" unexpected

但是,当我跑步时

bash go.sh

ImageMagick在一幅蒙太奇中打开文件夹中的所有图像,并且出现以下错误

go.sh: line 12: magick: command not found

2 个答案:

答案 0 :(得分:1)

类似这样的东西:

#!/bin/bash

# Get list of files into array - just once at start
files=(*.jpg)

# Do forever
first=0
while :; do
   # Shuffle array
   files=( $(shuf -e "${files[@]}") )

   # Make montage of first 5 images in shuffled array
   magick montage ${files[0]} ${files[1]} ${files[2]} ${files[3]} ${files[4]} montage.jpg

   # Start displaying if first pass - leaving "display" running in background updating itself every second
   if [ $first -eq 0 ] ; then
      display -update 1 montage.jpg &
      first=1
   fi

   # Snooze 10 and repeat
   sleep 10
done

或者这可能更容易理解:

#!/bin/bash

# Get list of files into array
files=(*.jpg)

montage=/tmp/montage.jpg

# Create initial "Loading sign"
convert -background black -fill white -pointsize 64 label:"Loading" $montage
display -update 1 $montage &

# Do forever
while :; do
   # Shuffle array
   files=( $(shuf -e "${files[@]}") )

   # Make montage
   magick montage ${files[0]} ${files[1]} ${files[2]} ${files[3]} ${files[4]} $montage

   # Snooze 10 and repeat
   sleep 10
done

macOS的用户可能没有内置于ImageMagick的X11支持,他们可以使用 homebrew 这样安装feh,也可以使用shuf(实际命令为gshuf )从GNU coreutils:

brew install feh
brew install coreutils

并尝试以下版本:

#!/bin/bash

# Get list of files into array
files=(*.jpg)

montage=/tmp/montage.jpg

# Create initial "Loading sign"
convert -background black -fill white -pointsize 64 label:"Loading" $montage

# Display "feh" running continuously in background passing the same image twice so we can cycle through the list!
feh --title "My Funky Montage" $montage $montage &
fehpid=$!

# Do forever
while :; do
   # Shuffle array
   files=( $(gshuf -e "${files[@]}") )

   # Make montage
   magick montage ${files[0]} ${files[1]} ${files[2]} ${files[3]} ${files[4]} $montage

   # Cycle feh to next image
   kill -s USR1 $fehpid

   # Snooze 10 and repeat
   sleep 10
done

答案 1 :(得分:0)

您要合并图像吗?在这种情况下,您可以执行以下操作:

app.scss