循环重命名脚本,使用 ImageMagick 将 3 个文件转换为蒙太奇,然后将文件发送到另一个文件夹
Loop a renaming script, convert 3 files using ImageMagick into a montage, and then sending the file to another folder
我目前正在使用 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 命令放在 "perform 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=
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"
我目前正在使用 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 命令放在 "perform 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=
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"