imagemagick-convert 是否可以在处理图像时动态计算旋转角度?

Is it possible with imagemagick-convert to dynamically calculate a rotation angle while processing images?

我想在多张图片上斜放水印。由于这些图像的尺寸各不相同,但水印应始终以恒定比例具有最大可能尺寸,因此我必须计算调整大小的最佳角度。 (应该看起来像 that. Not like that。)

我使用以下算法:

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

有关详细说明,请参阅

有没有办法在图像处理过程中动态地进行这个计算?

我在 Ubuntu Linux.
上使用 ImageMagick 6.8.9-9 Q16 x86_64 在 Bash 中它可能看起来像这样(没有调整大小):

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"

代码不旋转图像。我认为那是因为“-rotate”不接受“%[fx:]”参数?不幸的是,到目前为止我还没有找到关于这方面的明确信息...... 此外,变量 "w" 和 "h" 似乎具有值“0”......我也不明白。

此致
阿福

为了完整起见,以下是在 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"

此方法需要 IM7。

此致
阿福