Page 1 of 1

Add watermark to several files with different sizes from command line

Posted: 2019-08-30T17:08:39-07:00
by ElPeta
Hi all,

First time user, loving IM so far!

I am creating a batch script (Windows) that programatically adds the same watermark to many pictures. My script already works, however, since the pictures are different sizes, watermark is not consistently added at the same position. Also, I need the watermark to have the same ratio in relation to the picture's size, which is currently not happening with my script.

Here is what it currently looks like:

Code: Select all

@echo off
for /L %%i in (1,1,8) do ^
composite -dissolve 70% ^
-geometry -200-100 ^
-gravity southwest ^
Logo/Logo_Watermark.png ^
SourceFolder/IMG%%i.jpg OutputFolder/IMGwm%%i.jpg
Any help would be greatly appreciated! Thanks in advance!

--IM Version: 7.08-62

Re: Add watermark to several files with different sizes from command line

Posted: 2019-08-30T18:25:44-07:00
by fmw42
You will have more flexibility using magick convert syntax rather than magick composite syntax. In IM 7 replace convert with magick and composite with magick composite. But magick by itself is much more flexible than magick composite. You can use parenthesis processing to scale and offset the watermark image appropriately and you can compute that in-line with -set option. See convert documentation at:

https://imagemagick.org/Usage/compose/#compose
https://imagemagick.org/Usage/compose/#dissolve
https://imagemagick.org/Usage/basics/#parenthesis
https://imagemagick.org/Usage/basics/#set

https://imagemagick.org/script/porting.php#cli

syntax for a basic watermark would be

Code: Select all

magick img.jpg watermark.png -gravity southwest -geometry -200-100 -define compose:args=50,100 -compose dissolve -composite result.jpg

Here are two examples in Unix syntax where I automatically scale the watermark to 30% the size of the input image.

Input:
Image

Image

Watermark:
Image

Code: Select all

magick dragon_sm.gif -set option:sz "%[fx:min(w,h)*30/100]" \
\( star.gif -resize "%[sz]" \) \
-alpha on -channel rgba -gravity South \
-define compose:args=50,100 -composite dragon_star2d.gif
Image

Code: Select all

magick lena.png -set option:sz "%[fx:min(w,h)*30/100]" \
\( star.gif -resize "%[sz]" \) \
-alpha on -channel rgba -gravity South \
-define compose:args=50,100 -composite lena_star.png
Image


The Windows syntax would be:

Code: Select all

magick dragon_sm.gif -set option:sz "%[fx:min(w,h)*30/100]" ^
( star.gif -resize "%[sz]" ) ^
-alpha on -channel rgba -gravity South ^
-define compose:args=50,100 -composite dragon_star2d.gif

magick lena.png -set option:sz "%[fx:min(w,h)*30/100]" ^
( star.gif -resize "%[sz]" ) ^
-alpha on -channel rgba -gravity South ^
-define compose:args=50,100 -composite lena_star.png
If in a .bat file, then double the % to %%

See
https://imagemagick.org/Usage/windows/
https://imagemagick.org/script/fx.php

Re: Add watermark to several files with different sizes from command line

Posted: 2019-08-30T23:06:39-07:00
by ElPeta
Thanks so much, Fred! That worked like a charm! :D