使用 Imagemagick 切掉图像的 4 个边

Chop 4 sides of image with Imagemagick

我有以下图片:

test PNG 1366x655 1366x655+0+0 8-bit sRGB 286KB 0.000u 0:00.000

我需要从图像的边缘像这样切掉它:

top: 140 px
bottom: 140 px
left: 180 px
right: 60 px

是否有一个单行命令行可以用 convert 执行此操作?

您可以组合两个 -crop

                      #left,top      right,bottom
convert test.png -crop +180+140 -crop -60-140 cropped.png

that other guy的解决方案非常聪明。标准方法是使用 -chop。但这意味着 4 次调用,因为要删除的大小不对称。所以在使用-chop的ImageMagick中,你可以做

convert text.png -gravity north -chop 0x180 -gravity east -chop 60x0 -gravity south -chop 0x140 -gravity west -chop 140x0 cropped.png

http://www.imagemagick.org/Usage/crop/#chop

另见 -shave 当 left/right 或 top/bottom 或周围对称时。 http://www.imagemagick.org/Usage/crop/#shave

另一种使用 V7 的方法

magick input -crop "%[fx:w-(180+60)]"x"%[fx:h-(140+140)]"+180+140 result

基于 Bonzo 的解决方案,您可以使用视口裁剪(Unix 语法)在 ImageMagick 6 中执行类似的操作:

top=140
bottom=140
left=180
right=60
convert image.png -set option:distort:viewport "%[fx:w-$left-$right]x%[fx:h-$top-$bottom]+${left}+${top}" -filter point +distort SRT 0 +repage result.png