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
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