使用Imagemagick剪切图像的四个边角

9

我有以下图片:

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完成这个任务?

2
恭喜你们在 SO 上达成了第 100,000 个 Bash 问题! - that other guy
哇!谢谢你,伙计 =D - Gery
4个回答

24

您可以结合两个-crop选项:

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

2
非常好的解决方案,它完美地工作了,谢谢!只是想指出,应该是 convert test.png -crop +180+140 -crop -60-140 cropped.png, 考虑到左边(+180像素),上边(+140像素),右边(-60像素)和下边(-140像素)。再次感谢。 - Gery

5

那个人提出的解决方案非常巧妙。标准的方法是使用-chop命令。但是因为要删除的大小没有对称性,所以需要调用4次。因此,在ImageMagick中使用-chop时,可以这样做:

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。请参阅http://www.imagemagick.org/Usage/crop/#shave 了解更多信息。

2

使用V7的另一种方法

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


1

在Bonzo的解决方案基础上,您可以在ImageMagick 6中使用视口裁剪来实现类似的功能(Unix语法):

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接