bash - 循环重命名脚本,使用 ImageMagick 将 3 个文件转换为蒙太奇,然后将文件发送到另一个文件夹
问题描述
我目前正在使用 Raspberry Pi 3B,并想创建一个 bash 脚本,该脚本将在后台持续运行,等待:
在名为 PHOTOS 的文件夹中,包含名称为 photobooth_shot_00001.jpg、photobooth_shot_00002.jpg、photobooth_shot_00003.jpg 等的连续 .jpg。
我想将每 3 个新生成的 .jpg 重命名为“GROUP-1-1.jpg”、“GROUP-1-2.jpg”、“GROUP-1-3.jpg”、WITH GROUP-*- [1-3].jpg 依次递增。
然后,使用 ImageMagick 使用以下 bash 脚本将每个“GROUP-*”中的每 3 张图片转换为蒙太奇:
convert \
\( -size 600x400 xc:white \) \
\( GROUP-*-1.jpg -resize 370x \) -geometry +19+29 -compose over -composite \
\( GROUP-*-2.jpg -resize 187x \) -geometry +398+29 -compose over -composite \
\( GROUP-*-3.jpg -resize 187x \) -geometry +398+175 -compose over -composite \
\( watermark.png \) -geometry +0-10 -compose over -composite \
-density 100 GROUP-*-RESULT.jpg
最后,将“GROUP-*-RESULT.jpg”发送到另一个名为“PRINT”的文件夹
只是试图重新命名它们真的很难......谢谢你的帮助!
解决方案
假设您缺少的部分是如何重命名图像文件,请尝试以下操作:
#!/bin/bash
photodir="PHOTOS" # dir to store originally generated images
donedir="$photodir/done" # dir to backup processed files
seqfile="$photodir/seq" # file to keep the sequential group number
# read file names in $photodir, sort in increasing order and store in an array
while read -r -d "" f; do
ary+=("$f")
done < <(find "$photodir" -type f -name "photobooth_shot_*.jpg" -print0 | sort -z)
# abort if #files is less than three
if (( ${#ary[@]} < 3 )); then
echo "hoge"
exit
fi
# read last group number if the file exists
if [[ -f $seqfile ]]; then
seq=$(<"$seqfile")
else
seq=0
fi
seq=$((++seq))
mkdir -p "$donedir"
# rename top three files
for ((i=0; i<3; i++)); do
newname=$(printf "%s/GROUP-%d-%d.jpg" "$photodir" "$seq" "$((++i))")
newary+=("$newname")
mv -- "${ary[$i]}" "$newname"
done
# perform ImageMagick "convert" here
# for the files "GROUP-${seq}-[1-3].jpg" or "${newary[@]}"
# backup processed files
mv -- "${newary[@]}" "$donedir"
# store the group number for next invocation
echo "$seq" > "$seqfile"
- 请将您的 ImageMagick 命令放在“执行 ImageMagick”行周围。
- 该脚本应该被定期调用(例如,每 1 分钟左右,取决于新图像生成的频率)。
- 该脚本不应同时(并行)执行。
希望这可以帮助。
[编辑]
下面的脚本是根据您的要求的更新版本:
#!/bin/bash
photodir="PHOTOS" # dir to store originally generated images
donedir="$photodir/done" # dir to backup processed files
seqfile="$photodir/seq" # file to keep the sequential group number
# wait for the growing file to be completed by watching the filesize
# it may not be 100% reliable depending on the file creation process
waitfile() {
file=$1
if [[ ! -f "$file" ]]; then
return
fi
while true; do
size0=$(stat -c %s "$file")
sleep 1
size1=$(stat -c %s "$file")
sleep 1
size2=$(stat -c %s "$file")
if (( $size0 == $size1 && $size1 == $size2 )); then
return
fi
done
}
# exit immediately if the same name process still exists
# (the previous execution is still working..)
scriptname=${0##*/}
count=$(ps -aux | grep "$scriptname" | wc -l)
if (( $count > 3 )); then
exit
fi
# read file names in $photodir, sort in increasing order and store in an array
while read -r -d "" f; do
ary+=("$f")
done < <(find "$photodir" -type f -name "photobooth_shot_*.jpg" -print0 | sort -z)
# abort if #files is less than three
if (( ${#ary[@]} < 3 )); then
# echo "hoge" # this line is for debugging purpose
exit
fi
# find auto-composite photos
while read -r -d "" f; do
ary2+=("$f")
done < <(find "$photodir" -type f -name "photobooth[0-9]*.jpg" -print0 | sort -z
)
# abort if no auto-composite photos are found
if (( ${#ary2[@]} < 1)); then
# echo "hoge" # this is for debugging purpose
exit
fi
composite="${ary2[0]}"
waitfile "$composite" # wait for the auto-composite file to be complete
# read last group number if the file exists
if [[ -f $seqfile ]]; then
seq=$(<"$seqfile")
else
seq=0
fi
seq=$((++seq))
mkdir -p "$donedir"
# rename top three files
for ((i=0; i<3; i++)); do
newname=$(printf "%s/GROUP-%d-%d.jpg" "$photodir" "$seq" "$((++i))")
newary+=("$newname")
mv -- "${ary[$i]}" "$newname"
done
# rename the auto-composite file
newname2=$(printf "%s/GROUP-%d-ORIGINAL.jpg" "$photodir" "$seq")
mv -- "$composite" "$newname2"
# perform ImageMagick "convert" here
# for the files "GROUP-${seq}-[1-3].jpg" or "${newary[@]}"
# move the auto-composite file to the designated folder
# mv -- "$newname2" "destdir"
# backup processed files
mv -- "${newary[@]}" "$donedir"
# store the group number for next invocation
echo "$seq" > "$seqfile"
推荐阅读
- php - Telegram REST API,在消息文本中发送换行符?
- php - 配置php-fpm nginx mysql 内存占用低
- jmeter - 如何在 JMETER 中打印或获取 HTTP POST 请求正文
- twitter-bootstrap - Angular 5 BS4 如何制作所需的垂直按钮组
- octobercms - OctoberCMS - 如何将一个 url-slug 用于不同的页面?
- gentics-mesh - 在 Mesh-UI 中恢复节点的版本
- javascript - 通过 AJAX 将 JSON 数据发送到服务器并返回 - 涉及的步骤
- excel - Excel VBA 直接连接器问题
- scala - 使用 azure-cosmosdb-spark 时库版本冲突
- javascript - 删除 iframe 小部件中的供电 - 不显示且 css 不起作用