Search code examples
image-processingimagemagickwatermarkimagemagick-convertmogrify

How can I batch apply different watermarks for horizontal and vertical pictures in ImageMagick?


How can I mass apply different watermarks (horizontal and vertical) on different images (horizontal and vertical)?

I have folder tree with hundreds of PNG files like this (as example):

modern
classic
balance

I have two watermarks:

watermark-horizontal.png
watermark-vertical.png

How can I apply horizontal watermark on horizontal photos and vertical watermark on vertical photos?

I can do it for a single photo like this:

convert watermark-horizonal.png some-horizontal.png result.png

How can I do the same for many?


Solution

  • Like this:

    #!/bin/bash
    for f in *.png
    do
       read w h <<< $(convert "$f" -ping -format "%w %h" info: )
       if [ $w -gt $h ]; then
          echo "$f is $h tall and $w wide (landscape)"
          convert watermark-horizonal.png "$f" "wm-$f"
       else
          echo "$f is $h tall and $w wide (portrait)"
          convert watermark-vertical.png "$f" "wm-$f"
       fi
    done
    

    If you want to recurse, you can do this:

    #!/bin/bash
    find . -name "*.png" -print0 | while read -d $'\0' f
    do
       read w h <<< $(convert "$f" -ping -format "%w %h" info: )
       if [ $w -gt $h ]; then
          echo "$f is $h tall and $w wide (landscape)"
       else
          echo "$f is $h tall and $w wide (portrait)"
       fi
    done
    

    Save in a file called go, then type the following in a Terminal

    chmod +x go       # Do this just ONCE to make script executable
    ./go              # Do this any number of times to run it
    

    By the way, I use the following command for my watermarking:

    composite -dissolve 50% -resize "1x1<" -gravity center copyright.png x.png x.png