ImageMagick convert rotates crop

The rotation option in the ImageMagick convert tool rotates the image, but adds a background color to fill in the blanks.

I look for a way to rotate, and then crop the largest rectangle containing the contents of the image. Is it possible with the help of convert ?

Edited by Mark Setchell ...

So, if your original rectangle is a chessboard created like this:

convert -size 512x512 pattern:checkerboard a.png 

enter image description here

and you rotate it 20 degrees like this

 convert -background fuchsia -rotate 20 a.png b.png 

enter image description here

Do you need the largest rectangle that fits on a chessboard and does not contain pink?

+6
source share
2 answers

You can get an approximation of the expected result with +repage and replace -rotate with -distort ScaleRotateTranslate :

 convert -background fuchsia -distort ScaleRotateTranslate 20 +repage a.png b.png 

Result

+2
source

After creating the image as indicated:

 convert -size 512x512 pattern:checkerboard a.png 

This seems to do the job:

 angle=20 ratio=`convert a.png -format \ "%[fx:aa=$angle*pi/180; min(w,h)/(w*abs(cos(aa))+h*abs(sin(aa)))]" \ info:` crop="%[fx:floor(w*$ratio)]x%[fx:floor(h*$ratio)]" crop="$crop+%[fx:ceil((ww*$ratio)/2)]+%[fx:ceil((hh*$ratio)/2)]" convert a.png -set option:distort:viewport "$crop" \ +distort SRT $angle +repage rotate_internal.png 

From here .

+2
source

Source: https://habr.com/ru/post/984213/


All Articles