Shell脚本使用ImageMagick打开随机jpeg [英] Shell script to open random jpegs with ImageMagick
问题描述
我有大量的jpeg.我想编写一个shell脚本,该脚本随机选择5张图像,然后使用imageMagick将它们放在蒙太奇中,然后打开此蒙太奇文件.我希望此过程每10秒发生一次.
I have a large batch of jpegs. I would like to write a shell script that selects 5 images randomly and then, using imageMagick, put them in a montage and then open this montage file. I would like this process to occur every 10 seconds.
我已经尝试过该脚本
for f in *.jpg
do
shuf -ezn 5 * | xards -0 -n1 | montage *.jpg | display montage.jpg
done
但它不起作用
它将打开所有图像,并显示以下错误消息
It opens all of the images and gives me the following error message
image.sh 7: image.sh: Xards: not found
go.sh脚本与图像保存在同一文件夹/目录中,现在看起来像这样:
the go.sh script is kept in the same folder/directory as the images and now looks like this:
#!/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在一幅蒙太奇中打开文件夹中的所有图像,并且出现以下错误
ImageMagick opens all images in the folder in one montage and I get the following error
go.sh: line 12: magick: command not found
推荐答案
类似以下内容:
#!/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
或者这可能更容易理解:
Or this might be easier to understand:
#!/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
,还可以使用GNU coreutils中的shuf
(实际命令为gshuf
):
Users of macOS may not have X11 support built into ImageMagick, they can install feh
using homebrew like this and also shuf
(actual command is gshuf
) from 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
这篇关于Shell脚本使用ImageMagick打开随机jpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!