Watermarking images with proportional watermark

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
arcterex
Posts: 2
Joined: 2018-10-15T23:38:05-07:00
Authentication code: 1152

Watermarking images with proportional watermark

Post by arcterex »

Hey guys, this has been driving me crazy for the last two days, I've tried just about every example here and NOTHING seems to work, so I'm hoping I'm just doing something silly here.

I have images being uploaded to a site of unknown dimensions. I have a pre-built watermark and I simply want to overlay it on the image at some arbitrary size (ie: bottom left corner at 10% height of the image). Typical watermark stuff.

What is happening is that the examples I've tried that *should* be auto-magically resizing the watermark relative to the image simply aren't doing that and are instead leaving the watermark the same size, so I get the uploaded image with the watermark the original size, and not scaling relative to the image at all. The same thing happens if I start with a larger watermark image - it stays it's original size on the image it's being overlaid on without scaling.

An example output image is here:
https://searcharchives.missionarchives. ... /05Tbp.jpg

This is the larger watermark image (https://searcharchives.missionarchives. ... rmark2.png).

The commands I've tried which all result in the image there (with the big watermark) are:

Code: Select all

convert $FILE1 $WATERMARK   -resize %[fx:u[0].w]x -gravity south -composite $FILE1
This example was originally '.h/5' to make the watermark file 1/5th the height of the image, but .h/5 and .h/2 gave exactly the same result :(

Code: Select all

convert $FILE1 \( $WATERMARK  -resize x%[fx:u[0].h/2] \) -gravity south -composite $FILE1

Code: Select all

convert $FILE1 $WATERMARK -resize "x%[fx:t?u[0].h/100:u[0].h]" -gravity south -composite $FILE1
These examples have all been pulled from the forums here, and all of them claim to work and do what I'm looking for, but it's almost like the '-resize' part of the command is simply ignored and the images are simply composited together.

Anyone able to help here?

Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31 - Linux
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Watermarking images with proportional watermark

Post by snibgo »

You could set the watermark size after reading just the main image. Then resize the watermark, in parentheses so only that image is resized.

Code: Select all

magick main.png -set option:WMSIZE %[fx:w/2]x%[fx:h/2] ( wm.png -resize %[WMSIZE] ) -composite out.png
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Watermarking images with proportional watermark

Post by GeeMack »

arcterex wrote: 2018-10-15T23:57:38-07:00Anyone able to help here?

Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31 - Linux
Inline FX expressions like "%[fx:w+h]" work almost everywhere in IM7 commands, but they only work properly in a few IM6 operations. Fortunately you can use FX expressions in IM6 with the "+distort" operator, which can be used to scale the watermark image. Consider this command...

Code: Select all

convert input.png watermark.png \
   +distort affine "0,0 0,0 %[w],%[h] %[fx:t?v.w*(u.h/v.h*0.1):s.w],%[fx:t?v.h*(u.h/v.h*0.1):s.h]" \
   -shave 1 -gravity southeast -geometry +20+20 -composite output.png
That reads the input and watermark images, scales the watermark to one tenth the height of the input image, then composites the watermark 20 pixels up and 20 pixels over from the southeast corner of the input.

If you want the watermark to be more or less than 1/10 the height of the input image, change both instances of "0.1" in those two FX expressions.

If you want the watermark somewhere other than 20 pixels up and over from the southeast corner, you can change the "-gravity" and/or "-geometry" settings as needed. Your example watermark image has a lot of blank space around it, so you may need to account for that.
arcterex
Posts: 2
Joined: 2018-10-15T23:38:05-07:00
Authentication code: 1152

Re: Watermarking images with proportional watermark

Post by arcterex »

I'll try that as soon as I'm home. Is the other solution to simply update to IM7? I thought I was at the latest version, but if the answer is "you're using commands that aren't supported" then it seems like that'd be another way to go.

Thank you!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Watermarking images with proportional watermark

Post by fmw42 »

snibgo's solution requires IM 7 to do inline computation for the -resize from the -set option. GeeMack's solution will work on IM 6, because distort understands these computations where -resize does not.

IM 7 has more features, but you must use magick rather than convert. There are a few other oddities and it is more strict about syntax. See https://magick.imagemagick.org/download/beta/
Post Reply