1
votes

I'd like to place a watermark diagonally over a number of images. Since the dimensions of these images vary, but the watermark should always have the maximal possible size at constant proportions, I have to calculate the perfect angle for the resizing. (It should look like that. Not like that.)

I use the following algorithm:

ratio1 = pic1_width / pic1_height
ratio2 = pic2_width / pic2_height
angle = atan ((ratio1 - ratio2) / (1 - ratio1 * ratio2))

For a detailed explanation see here.

Is there any way to do this calculation dynamically during image processing?

I'm using ImageMagick 6.8.9-9 Q16 x86_64 on Ubuntu Linux.
In Bash it might look something like this (without the resizing):

convert -background none -gravity center -density 300 "$pic" \
    \( "$wmark" -rotate "%[fx:atan(((u.w/u.h)-(v.w/v.h))/(1-(u.w/u.h)*(v.w/v.h)))]" \) \
    -compose over -composite "$result"

The code does not rotate the image. I think that's because "-rotate" does not accept "%[fx:]" arguments? Unfortunately, I have not been able to find clear information about this so far ... In addition, the variables "w" and "h" seem to have the value "0" ... which I also do not understand.

Best regards
AFoeee

1
You need to upgrade to v7 of ImageMagick to get the capability to do replacement in the -rotate parameter. You'd also need to use magick -background ... rather than convert -background ...Mark Setchell
Thanks for the advice. Meanwhile, I switched to IM7. A solution to my problem can be found under this thread in the ImageMagick forum.AFoeee

1 Answers

2
votes

For the sake of completeness, here is the solution that was created with help from the ImageMagick Community:

wmark="watermark.png"
file="some.pdf"
result="result.jpg"

rotation="%[fx:ratioUF=u.w/u.h; ratioWM=v.w/v.h; t*(atan((ratioUF-ratioWM)/(1-ratioUF*ratioWM))*180/Pi)]"

magick  -define registry:temporary-path=/tmp/imagemagick \
        -background none \
        -density 300 \
        "$file" \
        -bordercolor none -border 1x1 -trim +repage \
        "$wmark" \
        -rotate "$rotation" \
        -resize "%[fx:u.w]x%[fx:u.h]" \
        -compose over -gravity center -composite \
        -background white \
        -flatten \
        "$result"

This approach requires IM7.

Best regards
AFoeee