resize the watermark image accordingly to the width of the target image

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
eugenepik
Posts: 2
Joined: 2017-09-29T17:00:14-07:00
Authentication code: 1151

resize the watermark image accordingly to the width of the target image

Post by eugenepik » 2017-09-29T17:16:46-07:00

I'd like to get help how to resize the watermark.png image accordingly to the target image that receives that watermark.
Version: ImageMagick 6.9.7-4 Q16 x86_64

The following script applies the same size of the watermark.png to all large and small images.

Code: Select all

find -mtime -1 -printf "%f\n"|awk -F\/ '{print "/usr/bin/composite -compose overlay -gravity SouthEast -geometry +5+5 watermark.png "$(NF)" "$(NF)}'|sh
Is it possible to modify this script to resize watermark.png based on a % of the target image width?

Thanks!

User avatar
fmw42
Posts: 22086
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: resize the watermark image accordingly to the width of the target image

Post by fmw42 » 2017-09-29T18:16:54-07:00

In IM 6, you will have to get the size of the other image in a separate step and scale it as desired and put it into a variable that can be used for your resize. You can use %[fx: ... ] calculations to do that and put it into a variable in one step. Then resize your watermark image. However, to add -resize to your command, you need to change from using the old composite command to the convert command, so that you can use parenthesis processing to resize the watermark image.

In IM 7, you can do it all in one command line.

Perhaps if you tell us how you want to scale the watermark image according to the other image's size, we can be more specific in the two commands needed to replace your composite command.

eugenepik
Posts: 2
Joined: 2017-09-29T17:00:14-07:00
Authentication code: 1151

Re: resize the watermark image accordingly to the width of the target image

Post by eugenepik » 2017-09-29T22:22:57-07:00

Unfortunately Debian 9 doesn't come with a IM 7 package. Thanks for the tip re: IM7, I installed it from source.

Now I have /usr/local/bin/convert
Version: ImageMagick 7.0.7-4 Q16 x86_64 2017-09-30

I also reviewed my images and found that out of all different sizes, I want to apply the watermark only as the following:

1) images 565 or 1024 px wide will receive a watermark sized 80x60
2) large images 2048 px wide will receive a watermark sized 280x210

I have both watermark files and their larger transparent PNG source file.

All other images in the same directories should be ignored

User avatar
fmw42
Posts: 22086
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: resize the watermark image accordingly to the width of the target image

Post by fmw42 » 2017-10-01T11:37:33-07:00

I believe that you will have to test each image for size, if you do not want to process particular size input images. So this kind of problem is most likely solved in IM 6 or IM 7 by creating a loop over you background images and testing the size and as appropriate skipping those whose size is not of concern and processing the ones that match either of your conditions. So you should put all your background images in one folder with the watermark images outside that folder or in a separate folder. The loop over each image in the background folder and do tests on size. Then run the pertinent convert or magick command to resize the watermark image (or choose from pre-sized versions) to do the watermarking.

User avatar
fmw42
Posts: 22086
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: resize the watermark image accordingly to the width of the target image

Post by fmw42 » 2017-10-01T12:04:54-07:00

An alternate approach (not tested) would be the following in IM 7. I test for your width and set the image sequence number as an index for your list of pre-sized watermark images, plus a transparent image to handle the case of "no watermark". It does process in the latter case, but just puts a transparent image as the watermark. You need to specify the brightness for the watermark and optionally any saturation.

Code: Select all

magick backgroundimage watermark1 watermark2 xc:none \
-set option:watermark_id "%[fx:(w==565)||(w==1024)?1:(w==2048)?2:3]" \
\( -clone 0 -clone "%[watermark_id]" -compose modulate \
-define compose:args={brigthness}[,{saturation}] -composite \) \
-delete 0-3 resultimage
See http://www.imagemagick.org/Usage/compose/#watermark for the convert/magick syntax for watermarking.

User avatar
GeeMack
Posts: 469
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: resize the watermark image accordingly to the width of the target image

Post by GeeMack » 2017-10-01T19:58:07-07:00

fmw42 wrote:
2017-10-01T12:04:54-07:00
An alternate approach (not tested) would be the following in IM 7. [...]

Code: Select all

magick backgroundimage watermark1 watermark2 xc:none \
-set option:watermark_id "%[fx:(w==565)||(w==1024)?1:(w==2048)?2:3]" \
\( -clone 0 -clone "%[watermark_id]" -compose modulate \
-define compose:args={brigthness}[,{saturation}] -composite \) \
-delete 0-3 resultimage
Here's another very similar idea (works on IM7 in Windows, not tested in *nix)...

Code: Select all

magick input.png wm_sm.png wm_lg.png xc:none \
   -duplicate "1,%[fx:(w==565)||(w==1024)?1:(w==2048)?2:3]" -delete 1-3 \
   -gravity southeast -geometry +5+5 -compose blend -define compose:args=50 -composite output.png
That builds a stack from the main input, both watermarks, and a transparent canvas. Then based on the main input width it makes a single duplicate of the small watermark, the large watermark, or the transparent canvas. Now the input image is first in the stack and the correct watermark is last, so get rid of everything between with "-delete 1-3". Then set gravity, geometry, blending level, etc., and finish with "-composite" and an output file name.

Edited: Corrected a correction.
Last edited by GeeMack on 2017-10-01T20:47:36-07:00, edited 1 time in total.

User avatar
fmw42
Posts: 22086
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: resize the watermark image accordingly to the width of the target image

Post by fmw42 » 2017-10-01T20:43:45-07:00

GeeMack wrote:It looks like the above code is missing a closing parenthesis.
Thanks. I have added the missing paren.

Post Reply