Search code examples
imagemagickbatch-processingimagemagick-convert

ImageMagick apply watermark multiple times


Currently, with ImageMagick I apply watermark.png on top of picture.jpg in the center using:

convert -size 700x1300 -composite picture.jpg watermark.png -gravity center -geometry 700x1300 output.jpg

This nicely overlays my watermark in the middle of the image. However, I want to achieve this effect in the top-left, center, and bottom-right. I've tried combining multiple -composites with no result. I would like to avoid calling 3 different commands to avoid the extra overhead it introduces, but I'm starting to believe it isn't possible without doing so.

What can I do to achieve the three watermark positions in one command, NorthWest, Center and SouthEast?


Solution

  • In Imagemagick, there is a -gravity setting that allows you to put the watermark in various locations specified by the compass directions. See http://www.imagemagick.org/script/command-line-options.php#gravity. However, your syntax is not proper and may fail for use with IM 7. The proper syntax is to read the input image first. So your command should be

    convert background watermark -gravity center -compose over -composite output
    

    Change center to one of the other gravity settings such as northwest or southeast

    Also your -size does nothing in your command. If you want to use -size WxH xc:somecolor, then you would use that in place of the background.

    If you want to resize, then do not use -geometry. That is typically used for offsetting the gravity placement. Use -resize WxH if you want to resize the result.

    See http://www.imagemagick.org/Usage/compose/#compose http://www.imagemagick.org/Usage/layers/#convert

    I would modify GeeMack's answer just slightly so it reads a bit more symmetric:

    convert watermark.png -write mpr:watermark +delete \
    input.png \
    mpr:watermark -gravity center -compose over -composite \
    mpr:watermark -gravity northwest -compose over -composite \
    mpr:watermark -gravity southeast -compose over -composite \
    output.png
    

    EDITED: to followup with OP question. If you need to resize the watermark, then do

    convert watermark.png -resize 700x1300 -write mpr:watermark +delete \
    input.png \
    mpr:watermark -gravity center -compose over -composite \
    mpr:watermark -gravity northwest -compose over -composite \
    mpr:watermark -gravity southeast -compose over -composite \
    output.png