ディレクトリツリーでimagemagick設定コマンドを繰り返し実行します。

ディレクトリツリーでimagemagick設定コマンドを繰り返し実行します。

私はそれぞれ、多数の画像を含む多くのサブフォルダを持つ大きなディレクトリツリーを持っています。ディレクトリ内のすべての画像に透かしを適用して、元の画像を上書きするスクリプトを見つけました。私はすべてのフォルダとサブフォルダを繰り返し、Linuxサーバー上の各画像に対して「作成」コマンドを実行するための最良の方法を探しています。私が見つけた元のスクリプトは次のとおりです。

#!/bin/bash

###########################################
# NAME:     wn-ow
# AUTHOR:   Linerd (http://tuxtweaks.com), Copyright 2009
# LICENSE:  Creative Commons Attribution - Share Alike 3.0 http://creativecommons.org/licenses/by-sa/3.0/
#       You are free to use and/or modify this script. If you choose to distribute this script, with or
#       without changes, you must attribute credit to the author listed above.
# REQUIRES: ImageMagick, coreutils
# VERSION:  1.0
# DESCRIPTION:  A script to add a watermark and overwrite all images in a directory.
#
# This script will watermark all of the images in a directory. Warning! This will overwrite the files.
###########################################

# Initialize variables
WM=$HOME/public_html/image/catalog/logo-website/watermark.png  # This is the path to your watermark image
SCALE=100                          # This sets the scale % of your watermark image

# Warning
echo -e "This will overwrite all of the images in this directory."\\n"Are you shure want to continue? {Y/n}"
read REPLY

if
    [ "$REPLY" != "n" ] && [ "$REPLY" != "N" ]
then
    file -i * | grep image | awk -F':' '{ print $1 }' | while read IMAGE
        do
            echo Watermarking $IMAGE
            composite -dissolve 40% -gravity SouthEast -quality 100 \( $WM -resize $SCALE% \) "$IMAGE" "$IMAGE"
        done
else
    echo exiting
    exit 0
fi

exit 0

「find . -name *.jpg」または他の組み合わせを使用する必要がありますか?

ベストアンサー1

スクリプトはすでにイメージファイルのディレクトリを確認していますが、各ディレクトリにメッセージを表示せずにすべてのディレクトリを繰り返すように調整したい場合は、それを上書きすることもできます。それは次のとおりです。

#!/bin/bash

WM=$HOME/public_html/image/catalog/logo-website/watermark.png  # This is the path to your watermark image
SCALE=100                          # This sets the scale % of your watermark image
STARTDIR="/home/whatever/images"   # This is the directory to start in

for imagedir in $( find $STARTDIR -type d )
do
  echo "Running in $imagedir ..."
  cd $imagedir
  file -i * | grep image | awk -F':' '{ print $1 }' | while read IMAGE
  do
    echo "Watermarking $IMAGE"
    composite -dissolve 40% -gravity SouthEast -quality 100 \( $WM -resize $SCALE% \) "$IMAGE" "$IMAGE"
  done
done

おすすめ記事