BASH SCRIPT - 为 *** 中的图片执行(仅一次 - 并非所有图片始终)CRON 作业

BASH SCRIPT - for image in *** do (Only once - not all pictures all the time) CRON job

我有一个脚本可以使用 imagemagick 为我的图像加水印。我已将我的脚本设置为 bash 作业,但它始终为每张图片加水印。我想排除已经加水印的图片,但我没有将所有带水印的图片移出某个文件夹的选项。文件夹 A 包含原始图像。脚本扫描文件夹 A 以查找 png、jpg 和 gif 图像,并为它们添加水印 - 然后将原始图片移动到子文件夹。每次我的脚本扫描文件夹 A 时,它都会为所有已经加水印的文件加水印。而且我无法更改文件的名称。有没有办法通过将它们添加到文件数据库或其他东西来检查带水印的文件?我的脚本如下:

#!/bin/bash

 savedir=".originals"

 for image in *png *jpg *gif do  if [ -s $image ] ; then   # non-zero
 file size
     width=$(identify -format %w $image)
     convert -background '#0008' -fill white -gravity center \
        -size ${width}x30 caption:'watermark' \
        $image +swap -gravity south -composite new-$image
      mv -f $image $savedir
      mv -f new-$image $image
     echo "watermarked $image successfully"   fi done

以下示例说明如何modify/update您当前的脚本添加一种本地数据库文件以跟踪已处理的文件:

#!/bin/bash

savedir=".originals"

PROCESSED_FILES=.processed
# This would create the file for the first time if it 
# doesn't exists, thus avoiding "file not found problems" 
touch "$PROCESSED_FILES"

for image in *png *jpg *gif; do 
    # non-zero
    if [ -s $image ]; then  
        # Grep the file from the database
        grep "$image" "$PROCESSED_FILES"

        # Check the result of the previous command ($? is a built-in bash variable 
        # that gives you that), in this case if the result from grep is different
        # than 0, then the file haven't been processed yet
        if [ $? -ne 0 ]; then

            # Process/watermark the file...

            width=$(identify -format %w $image)
            convert -background '#0008' -fill white -gravity center -size ${width}x30 caption:'watermark' $image +swap -gravity south -composite new-$image
            mv -f $image $savedir
            mv -f new-$image $image

            echo "watermarked $image successfully"   

            # Append the file name to the list of processed files
            echo "$image" >> "$PROCESSED_FILES"
        fi 
    fi 
done

就我个人而言,我不希望需要一些其他的外部数据库来记录我已加水印的图像的名称 - 如果该文件与图像分离,如果它们被移动到不同的文件夹层次结构或重命名怎么办?

我的偏好是在图像内设置注释,以标识每张图像是否有水印 - 然后信息随图像一起传播。所以,如果我给图片加水印,我会在评论中设置它

convert image.jpg -set comment "Watermarked" image.[jpg|gif|png]

然后,在我加水印之前,我可以用 ImageMagick 的 identify 检查它是否完成:

identify -verbose image.jpg | grep "comment:"
Watermarked

显然,您可以更复杂一些,提取当前评论并添加 "Watermarked" 部分,而不覆盖其中可能已经存在的任何内容。或者你可以在给图片加水印的时候设置图片的IPTC author/copyright持有人或者版权信息,以此作为图片是否加水印的标志。