Overlaying a watermark/logo on multiple images of multiple dimensions

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
instrumentally
Posts: 6
Joined: 2018-04-02T07:33:17-07:00
Authentication code: 1152

Overlaying a watermark/logo on multiple images of multiple dimensions

Post by instrumentally »

I have an image (JPG format) that I wish to "brand" (like a cowboy branding a cow) with a logo, center justified. I want to preserve the original. IOW, I want a new copy of the original base image with the branding/watermark placed on top of the new copy, saved off into a separate target folder. The resulting new image will be of the same dimensions as the original base file.

I understand that ImageMagick does allow two images to be merged/combined, but I fail to find anything in the documentation that addresses how to scale one image (the overlaid branding logo) to match (roughly) the dimensions of the underlying image.

The image that will overlay the other will have a transparent background. If need be, I can create a SVG for this purpose, so that it will scale without distortion. My question is, therefore, how to get the "branding" image to reflect (roughly) the dimensions of the base image. I surely don't want to place a 1000x1000 "branded" logo over top of a 300x300 base image. Nor would I want to have a 100x100 logo placed on 3000x3000 image. I would like to be able to maintain a fairly consistent ratio between the overlaid image with the base image. It doesn't have to be perfect, but I don't want the overlaid image to be chopped off by the original frame dimensions of the base image, nor do I want the ovelaid image to become a small blip on the base image.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Overlaying a watermark/logo on multiple images of multiple dimensions

Post by Bonzo »

What version of Imagemagick are you using?
Have you tried searching the forum as there were similar post last year?

viewtopic.php?f=1&t=30890&p=150443&hili ... rk#p150443
viewtopic.php?f=1&t=32784&p=150159&hili ... rk#p150159
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Overlaying a watermark/logo on multiple images of multiple dimensions

Post by fmw42 »

Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at http://www.imagemagick.org/discourse-se ... f=1&t=9620

For novices, see

http://www.imagemagick.org/discourse-se ... f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
https://github.com/ImageMagick/usage-markdown

See for example:
https://www.imagemagick.org/Usage/compose/#watermark
https://www.imagemagick.org/Usage/layers/#convert

If you provide your two input images, we can help further.

Image
Image

(unix syntax for IM 6)

Code: Select all

pct=33
amt=`convert -ping monet2.jpg -format "%[fx:$pct*min(w,h)/100]" info:`
convert monet2.jpg \( flatten_xor.png -resize $amt \) -gravity center -compose over -composite monet_watermark.png
Image
instrumentally
Posts: 6
Joined: 2018-04-02T07:33:17-07:00
Authentication code: 1152

Re: Overlaying a watermark/logo on multiple images of multiple dimensions

Post by instrumentally »

I am using version 7.0.1-Q16
pct=33
amt=`convert -ping monet2.jpg -format "%[fx:$pct*min(w,h)/100]" info:`
convert monet2.jpg \( flatten_xor.png -resize $amt \) -gravity center -compose over -composite monet_watermark.png
How are you supposed to accomplish the above on a Windows box at a c:> prompt (assuming I'm in the ImageMagick folder)?
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Overlaying a watermark/logo on multiple images of multiple dimensions

Post by GeeMack »

instrumentally wrote: 2018-04-03T19:21:14-07:00How are you supposed to accomplish the above on a Windows box at a c:> prompt (assuming I'm in the ImageMagick folder)?
Using IM7 at a Windows CMD prompt, a command like this will read in both images, resize the logo to fit within a box 90% the width and height of the base image, and overlay the resized logo centered on the base image.

Code: Select all

magick input.png logo.png ^
   -resize %[fx:t?u.w*0.9:u.w]x%[fx:t?u.h*0.9:u.h] -gravity center -composite output.png
To use this in a BAT script you'll need to change those single percent signs "%" into doubles "%%".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Overlaying a watermark/logo on multiple images of multiple dimensions

Post by fmw42 »

GeeMack: Would it not be better to just resize the logo. You spend effort resizing the input.png even though it does change it's size.

You can correct me here on my IM 7 Windows syntax using my examples above. It works in IM 7 Unix by adding \ before the parens.

Code: Select all

magick monet2.jpg -set option:dim "%[fx:max(u.w,u.h)*33/100]" ( flatten_xor.png -resize "%[dim]" ) -gravity center -compose over -composite monet_watermark.png
instrumentally
Posts: 6
Joined: 2018-04-02T07:33:17-07:00
Authentication code: 1152

Re: Overlaying a watermark/logo on multiple images of multiple dimensions

Post by instrumentally »

Thanks to everyone, however, the logo image I am trying to overlay is a SVG file, and despite the fact that the SVG has a transparent background, the overlaid image is coming in with a white background in the resulting output file. How do I maintain the SVG's transparent background in this operation? The image in question can be found here:

http://hostsafe.com/temp/in.svg
instrumentally
Posts: 6
Joined: 2018-04-02T07:33:17-07:00
Authentication code: 1152

Re: Overlaying a watermark/logo on multiple images of multiple dimensions

Post by instrumentally »

Also a problem is that even if I have my visible SVG elements defined with 50% opacity, the resulting magick merge ignores the opacity value. The above hyperlink to the SVG has 50% opacity for the two black elements found therein.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Overlaying a watermark/logo on multiple images of multiple dimensions

Post by GeeMack »

fmw42 wrote: 2018-04-03T23:44:10-07:00GeeMack: Would it not be better to just resize the logo. You spend effort resizing the input.png even though it does change it's size.
The FX expressions in my example resize the second image to 90% the size of the first, and resize the first image to its own size. I don't know the internals, but my understanding is that resizing to its same dimensions comes at almost no cost. A quick test shows your method might be 0.01 second faster, maybe because it's only doing one FX calculation. I don't know.
You can correct me here on my IM 7 Windows syntax using my examples above. It works in IM 7 Unix by adding \ before the parens.

Code: Select all

magick monet2.jpg -set option:dim "%[fx:max(u.w,u.h)*33/100]" ( flatten_xor.png -resize "%[dim]" ) -gravity center -compose over -composite monet_watermark.png
Yes, that works in Windows as-is.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Overlaying a watermark/logo on multiple images of multiple dimensions

Post by snibgo »

instrumentally wrote:How do I maintain the SVG's transparent background in this operation?
Prefix the svg name with "-background None".

So, using GeeMack's method, adding a few line-breaks for clarity, Windows CMD syntax:

Code: Select all

magick ^
  input.png ^
  -background none in.svg ^
  -resize %[fx:t?u.w*0.9:u.w]x%[fx:t?u.h*0.9:u.h] ^
  -gravity center -composite ^
  output.png
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Overlaying a watermark/logo on multiple images of multiple dimensions

Post by fmw42 »

If your svg renders small or is not good resolution, then for better quality add -density XX before in.svg with XX=300 or so, to be sure you get a high quality result. Note that too large a density will slow things down.
instrumentally
Posts: 6
Joined: 2018-04-02T07:33:17-07:00
Authentication code: 1152

Re: Overlaying a watermark/logo on multiple images of multiple dimensions

Post by instrumentally »

-background none
Dilemma...

If I have two images that I am working with, how does one handle the situation where the base picture is a PNG file with a transparent background however I want that transparent background placed on a white background AND...AND!!...AND...the watermark image being placed on top of the base picture is also a PNG with a transparent background and in this case, I want the background to remain transparent.

Using "-background none" doesn't accomplish both.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Overlaying a watermark/logo on multiple images of multiple dimensions

Post by snibgo »

You have a png image you want to flatten against white, then want to composite over that an svg with transparency. Is that correct? Then just do those operations:

Code: Select all

magick ^
  input.png ^
  -background White -layers Flatten ^
  -background none in.svg ^
  -resize %[fx:t?u.w*0.9:u.w]x%[fx:t?u.h*0.9:u.h] ^
  -gravity center -composite ^
  output.png
snibgo's IM pages: im.snibgo.com
instrumentally
Posts: 6
Joined: 2018-04-02T07:33:17-07:00
Authentication code: 1152

Re: Overlaying a watermark/logo on multiple images of multiple dimensions

Post by instrumentally »

Thank you to all who have helped.
Post Reply