82

I am trying to crop a 640x640 image using ImageMagick on the command line.

I need to remove about 20 pixels from the bottom part of the image all the way from left to right. A long strip along the bottom.

I know about the shave command.

What would be the command to enter in the command line? I am using the Windows version of this software.

Dave M
  • 13,250

5 Answers5

93

Assuming that you always know the size of your image you can do it like this:

convert original.jpg -crop 640x620+0+0 cropped.jpg

With the -crop operator you specify the size of the cut out image and the offset from the upper left corner of the old image.

In order to get rid of the 20px along the bottom, you have to choose a size of 640x620 and an offset of 0+0

Giacomo1968
  • 58,727
Alex Maiburg
  • 1,031
  • 6
  • 2
50

Use ImageMagick's -chop operator as follows to remove 20 rows of pixels from the bottom:

convert image.png -gravity South -chop 0x20 result.png

Change to -gravity North to chop top 20 rows.


Change to:

convert image.png -gravity East -chop 20x0 result.png

To crop from right side, note that the 20 pixels are now before the x separator.

Giacomo1968
  • 58,727
2

Actually it took me a wile to get this right, so I thought let's post it here.

"C:\Program Files\ImageMagick-7.0.10-Q8\magick" convert -crop 100x100+800+600 fileIn.png fileOut.png

so one need both "magick","convert" without "-" and then the options with "-". The history of this strange call is that convert used to be an executable (The only executable is magick). However since there was another windows system program with the same name "convert", it's now passed as an argument somehow. Moreover one needs to have the folder in path.

An easy batch to simplify the call could be:

crop fileIn.png fileOut.png 100,100,800,600

where the code would be

crop.bat

set fname1=%1 set fname2=%2 set x0=%3 set y0=%4 set w=%5 set h=%6 "C:\Program Files\ImageMagick-7.0.10-Q8\magick" convert -crop %w%x%h%+%x0%+%y0% %fname1% %fname2%

Massimo
  • 121
1

Crop centered in batches

for f in *.jpg; do convert "$f" -gravity center -crop 98x98+0+0 +repage "$f" ; done

Fulfil with -extent in a canvas -background white

for f in *.jpg; do convert "$f" -gravity center -crop 98x98+0+0 +repage -extent 98x98 -background white "$f" ; done
Giacomo1968
  • 58,727
Ax_
  • 111
1

You need the -crop option, but this is not sufficient, as with image formats like PNG, the image may have a virtual canvas and you generally do not want such a virtual canvas in the output image. So, in doubt, you should use a +repage before the crop in order to remove virtual canvas offset information possibly present in the input image, and you should always do a +repage after the crop to remove such information (generated by crop, in particular), i.e. assume that the input may not be clean and ensure that the output is clean. This gives:

convert in.png +repage -crop <Width>x<Height>+OffsetX+OffsetY +repage out.png

And in your case:

convert in.png +repage -crop 640x620+0+0 +repage out.png

At the end of the -crop ImageMagick documentation:

It might be necessary to +repage the image prior to cropping the image to ensure the crop coordinate frame is relocated to the upper-left corner of the visible image. Similarly you may want to use +repage after cropping to remove the page offset that will be left behind. This is especially true when you are going to write to an image format such as PNG that supports an image offset.

If you use -crop while there is a virtual canvas, you may get an incorrect result, possibly with a warning like "geometry does not contain image".

Giacomo1968
  • 58,727
vinc17
  • 131