繁体   English   中英

循环重命名脚本,使用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=$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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM