imagemagick watermark script

In a bash script (bash is now available on all three platforms: windows, mac and of course linux) Let’s say I want to apply a watermark to all images in a directory, in a loop, using imagemagick. Let’s say the watermark is a file named atcopyright.png (with a transparent background).

for file in *jpg
composite -gravity SouthWest atcopyright.png $file cpy_$file

…the above sort of does it. But if atcopyright.png has white lettering it’s invisible on a light background, and versa visa with dark.

Punchline question:
Is there a scriptable way to sample the SouthWest corner of the subject image, and choose between one of two files (atcopyrightLight.png or atcopyrightDark.png)…inside the for file in *jpg loop?

Perhaps an easier solution is to add a drop shadow to your logo to make it pop off of any color back ground :slight_smile:

1 Like

RE> drop shadow

Yes good idea. That too kinda/sorta works. But when only the shadow appears that too is a bit weird.

Perhaps two high contrast black and white letter sets, slightly offset in both X and Y axes, would work better than a (typically) fuzzy and blurred drop shadow.

I suggest you don’t use “composite”. Instead, use “convert” (for IM v6) or “magick” (for IM v7). These need the input files in the opposite order.

A bash script could be something like this:

magick \
  in.png \
  \( +clone \
     -gravity SouthWest -crop 100x20+0+0 +repage \
     -set option:DELWHICH "%[fx:mean<0.5?1:2]" \
     +delete \
  \) \
  copyL.png \
  copyD.png \
  -delete %[DELWHICH] \
  -compose Over -composite \

This finds the mean value in an area 100x20 in the bottom-left corner. If this is less than 0.5, it will composite copyD.png, which might be white characters on transparent background. Otherwise it composites copyL.png.

EDIT: This won’t work with IM v6. It needs v7.


I have a close G’MIC equivalent but need to rejigger for your purposes. Won’t have the time or energy however. Since we already have a magick :mage: answer, I guess we are okay without it. :stuck_out_tongue:

Thank you. I’ll try that.

RE> v7
For Mint linux I found the following:

tar -xvf ImageMagick.tar.gz
cd ImageMagick-7.0.*
./configure --prefix=/usr
sudo make install
sudo ldconfig /usr/local/lib

You can also do the tried and true method used by meme generators and put an outline around your text that’s the opposite (black outline on white lettering, or vice versa). This keeps it readable no matter the background.

1 Like

RE> white on black two tone watermark

Yes that’s probably best. I am pleased to know how to sample an image area in a script, to get an average grayscale value. That was my original question and I got the answer. Hail hail

1 Like

Captions should be easily legible. I like something like this:

magick \
  -size 300x50 xc:none \
  -gravity Center -pointsize 30 -annotate 0 snibgo \
  -blur 0x3 -auto-level \
  -fill White -annotate 0 snibgo -trim +repage \

Watermarks might be more subtle, eg the white might be semi-transparent.

Incidentally, ImageMagick has a forum, with a sub-forum for questions like this.

1 Like

Personally, I don’t like fuzzy blurred drop shadows. Outlines (in their many permutations) are more reliable.