【问题标题】:Is it possible with imagemagick-convert to dynamically calculate a rotation angle while processing images?是否可以使用 imagemagick-convert 在处理图像时动态计算旋转角度?
【发布时间】:2018-07-05 15:37:58
【问题描述】:

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

我使用以下算法:

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

详细解释见here

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

我在 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”……我也不明白。

最好的问候
AFeee

【问题讨论】:

  • 您需要升级到 ImageMagick v7 才能在-rotate 参数中进行替换。您还需要使用magick -background ... 而不是convert -background ...
  • 感谢您的建议。同时,我切换到IM7。我的问题的解决方案可以在 ImageMagick 论坛的this thread 下找到。

标签: image image-processing imagemagick image-manipulation imagemagick-convert


【解决方案1】:

为了完整起见,这里是在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。

最好的问候
AFeee

【讨论】:

  • 如果您将 -rotate 替换为 +distort SRT,这将适用于 IM 6 convert 和 IM 7 magick。在 IM 6 中,只有少数操作允许内联计算,其中之一是扭曲。见imagemagick.org/Usage/distorts/#srt。您还可以在 +distort SRT 中将调整大小作为一项操作包括旋转和缩放。
猜你喜欢
  • 2017-09-01
  • 1970-01-01
  • 2016-10-19
  • 2014-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
相关资源
最近更新 更多