8

I have the following image:

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

and I need to chop it like this from the border of the image:

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

Is there a one-line command line to do this with convert?

Gery
  • 8,390
  • 3
  • 22
  • 39

4 Answers4

23

You can combine two -crops:

                      #left,top      right,bottom
convert test.png -crop +180+140 -crop -60-140 cropped.png
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • 2
    great solution, it works perfectly, thanks!, just to point out that it should be `convert test.png -crop +180+140 -crop -60-140 cropped.png` considering the left (+180 px), top (+140 px), right (-60 px) and bottom (-140 px) directions. Thanks again. – Gery May 23 '18 at 19:11
5

The solution from that other guy is very clever. The standard way would be to use -chop. But that means 4 calls, since there is no symmetry in the sizes to be removed. So in ImageMagick using -chop, you could do

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

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

See also -shave when there is symmetry either left/right or top/bottom or all around. http://www.imagemagick.org/Usage/crop/#shave

fmw42
  • 46,825
  • 10
  • 62
  • 80
2

Another method using V7

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

Bonzo
  • 5,169
  • 1
  • 19
  • 27
1

Building upon Bonzo's solution, you can do something similar in ImageMagick 6 using viewport cropping (Unix syntax):

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
fmw42
  • 46,825
  • 10
  • 62
  • 80