ImageMagick转换旋转裁剪

Moh*_*imi 6 imagemagick imagemagick-convert

ImageMagick的转换工具中的旋转选项可旋转图像,但会添加背景颜色以填补空白.

我正在寻找一种旋转的方法,然后裁剪包含图像内容的最大矩形.转换是否可能?

Mark Setchell编辑...

所以,如果你的原始矩形是一个像这样创建的棋盘:

convert -size 512x512 pattern:checkerboard a.png
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

然后像这样将它旋转20度

convert -background fuchsia -rotate 20 a.png b.png
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

你想要最大的矩形适合棋盘并且不包含粉红色?

bfo*_*ine 5

您可以通过使用+repage并替换-rotate为来获得预期结果的近似值-distort ScaleRotateTranslate

convert -background fuchsia -distort ScaleRotateTranslate 20 +repage a.png b.png
Run Code Online (Sandbox Code Playgroud)

结果


Iva*_*aer 5

创建图像后,如图所示:

convert -size 512x512 pattern:checkerboard a.png
Run Code Online (Sandbox Code Playgroud)

这似乎可以完成工作:

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((w-w*$ratio)/2)]+%[fx:ceil((h-h*$ratio)/2)]"
convert a.png -set option:distort:viewport "$crop" \
          +distort SRT $angle +repage   rotate_internal.png
Run Code Online (Sandbox Code Playgroud)

这里开始