本文介绍了如何计算上覆图像的旋转角度,以便它只是对角地嵌入底层图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在许多照片上对角放置一个水印。这些图片的宽度和长度各不相同,有些甚至可能比水印本身小。



这将在脚本中使用,用户可以从中选择不同的选项如水平,垂直,对角线或由他/她自己指定角度。因此我需要计算旋转角度。

角度应




  • 旋转水印,使其对角线位于基础图像上方。

  • 以恒定比例达到最大可能尺寸。



它看起来像


I'd like to place a watermark diagonally over a number of pictures. The width and length of these pictures differ and some might even be smaller than the watermark itself.

This will be used inside a script, in which the user can choose from different options like "horizontal", "vertical", "diagonal" or specify an angle by himself/herself. Therefore I need to calculate the rotation angle.
The angle should

  • rotate the watermark, so that it is diagonally positioned over the underlying image.
  • achieve the maximal possible size at constant proportions.

It would look something like this. (I have also drawn the diagonal.)
As you can see, the overlying picture just fits inside the underlying picture. If i'd choose an other angle, the overlying image would become smaller.

I had already calculated the diagonal of the underlying image and then the corresponding angle. The problem is that the length is not necessarily maximized, as seen here.

I'm using ImageMagick 6.8.9-9 Q16 x86_64 on Ubuntu Linux.

Here is the bash code without the calculation (as i said, it didn't work well). Please note that the code is actually executed via Python3. I find that shell commands are easier to read in bash.

for pic in *
do
    # if there transparency around the picture, remove it and get the dimensions
    size_ui=`convert -density 300 "$pic" -page a4 -bordercolor none -border 1x1 -trim +repage jpg:- | identify -format "%wx%h" -`

    # get dimensions of the watermark (in case it changes over time)
    size_wm=`identify -format "%wx%h" "wm.png"`

    degree=`CALCULATE_THE_PERFECT_ANGLE`

    # center the watermark, rotate it and resize it to the same size as the underlying picture
    convert -density 300 "$pic" -page a4 \( "wm.png" -background none -gravity center -rotate "$degree" -resize "$size_ui" \) -compose over -composite "${pic%%.*}.jpg"
done

I am very sure that this problem has already been solved somewhere on stackoverflow, but could not find anything up to now.

Best regards
AFoeee

解决方案

Let we have base rectangle with width W and height H with ratio K=H/W and rotated rectangle with unknown width and height but fixed ratio k=h/w.

I marked similar angles f at the picture. We can see that

 H = h * cos(f) + w * sin(f)
 W = h * sin(f) + w * cos(f)
 or
 K * W = k * w * cos(f) + w * sin(f)
 W = k * w * sin(f) + w * cos(f)
 divide the first by the second
 K = (k * cos(f) + sin(f)) / (k * sin(f) + cos(f))
 divide denominator and nominator by cos(f)
 K = (k + tg(f)) / (k * tg(f) + 1)
 K * (k * tg(f) + 1) = k + tg(f)
 K * k * tg(f) + K = k + tg(f)
 tg(f) * (1 - K * k) = K - k

and result is

 tg(f)  = (K - k) / (1 - K * k)
 f = atan((K - k) / (1 - K * k))

Quick check: for square K=1 and for any k<1 tg(f)=1, so f = Pi/4 (45 degrees)

Note that not all combinations of K and k have sense

这篇关于如何计算上覆图像的旋转角度,以便它只是对角地嵌入底层图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:10