自动水印 - 文件夹操作

时间:2013-07-31 20:13:52

标签: macos bash imagemagick applescript

我有一个bash脚本,它会自动为文件夹中的每个图像添加水印。

WATERMARK="$HOME/Dropbox/logo_bw.png"
if [ ! -e "watermarked_tmp" ]
then
mkdir watermarked_tmp
fi
#loop inside all the images in folder
for image in *.jpg *.JPG *.jpeg *.JPEG *.png *.PNG
do
if [ ! -e "$image" ] # Check if file exists.
then
continue
fi
#Retrieve size of the image and divide the lenght by 2
size=`identify -format %[fx:w/4,279] $image`
#Apply the watermark and create a new image in the "watermarked" subfolder
composite  -dissolve 10% -quality 100 -gravity SouthEast -background none \( $WATERMARK -geometry ${size} \) ${image} watermarked_tmp/${image}
done

我想用Automator从这段代码创建一个Folder Action。有谁知道如何传递当前目录及其文件?我尝试过,但在for image in *.jpg *.JPG *.jpeg *.JPEG *.png *.PNG

行失败了

1 个答案:

答案 0 :(得分:4)

将传递输入设置为参数并使用类似for f; do的for循环:

watermark=~/a.png
d=~/watermarked_temp
mkdir -p $d
shopt -s nocasematch
for f; do
  [[ $f =~ .*\.(jpe?g|png)$ ]] || continue
  target="$d/${f##*/}"
  size=$(identify -format '%[fx:w/4,279]' "$f")
  composite -dissolve 10% -gravity SouthEast\
  \( $watermark -geometry $size \) "$f" -quality 100 "$target"
done