我有一个很大的目录树,其中有许多子文件夹,每个子文件夹中都有大量图像。我找到了一个脚本,可以将水印应用到目录中的所有图像,就地覆盖原始图像。我正在寻找迭代所有文件夹和子文件夹并为 Linux 服务器上的每个图像运行“compose”命令的最佳方法。这是我找到的原始脚本
#!/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