How to calculate the rotation angle for an overlying image, so that it just fits diagonally iniside an underlying image?
Posted: 2018-01-23T02:55:29-07:00
Hello,
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
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.
I am very sure that this problem has already been solved somewhere in the forum, but could not find anything up to now.
Best regards
AVoeee
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.
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.
Code: Select all
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
Best regards
AVoeee