Add watermark to several files with different sizes from command line

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
ElPeta
Posts: 2
Joined: 2019-08-30T16:48:48-07:00
Authentication code: 1152

Add watermark to several files with different sizes from command line

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
ElPeta
Posts: 2
Joined: 2019-08-30T16:48:48-07:00
Authentication code: 1152

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

Post by ElPeta »

Thanks so much, Fred! That worked like a charm! :D
Post Reply