循环重命名脚本,使用ImageMagick将3个文件转换为蒙太奇,然后将文件发送到另一个文件夹

时间:2019-01-09 08:16:27

标签: bash imagemagick

我当前正在使用Raspberry Pi 3B,并且想要创建一个bash脚本,该脚本将在后台持续运行,等待:

在名为PHOTOS的文件夹中,包含顺序的.jpg,其名称为photobooth_shot_00001.jpg,photobooth_shot_00002.jpg,photobooth_shot_00003.jpg等。

我想用名称“ GROUP-1-1.jpg”,“ GROUP-1-2.jpg”,“ GROUP-1-3.jpg” WITH GROUP重命名每3个新生成的.jpg。 -*-[1-3] .jpg顺序增加。

然后,使用ImageMagick使用以下bash脚本,将每个“组-*”中的每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”的文件夹

试图重命名它们真的很困难...谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

假设您缺少的部分是如何重命名图像文件,请尝试以下操作:

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