Shell 使用 ImageMagick 打开随机 jpeg 的脚本

Shell script to open random jpegs with 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 脚本保存在与图像相同的 folder/directory 中,现在看起来像这样:

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

你想拼图吗?在这种情况下,您可以执行以下操作:

while true; do    
sleep 10;
montage -mode concatenate -tile 1x $(ls | sort -R | tail -n 5 | paste -sd " " -) montage.jpg && display montage.jpg;
done

像这样:

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