54

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.

0

4 Answers 4

57

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

2
  • 16
    If you want to crop all the PNGs in your current directory, without keeping the original images: mogrify -crop 1280x1024+0+0 *.png. See also this blog entry on convert and mogrify. Nov 25, 2020 at 18:34
  • 3
    So, just to be clear, the syntax is width x height + originX + originY? Apr 30, 2022 at 3:15
33

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.

3
  • 1
    Remove the zero (as in 1x20) to actually remove anything
    – yPhil
    Sep 9, 2018 at 10:24
  • What is the purpose of the 0 for in the first place?
    – kojow7
    Feb 9, 2021 at 16:55
  • 2
    @kojow7 The 0 is not strictly required. I included it to demonstrate that the format is WIDTHxHEIGHT so that folks see the way to specify rows or columns which wouldn't have been clear if I had just said "use -chop x20". HTH. Feb 9, 2021 at 16:59
1

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%
2
  • This is actually incorrect. Version 7 ImageMagick should almost never be used in the format magick convert .... You should just use magick INPUTIMAGE .... OPERATIONS ... OUTPUTIMAGE unless you specifically want old Version 6 behaviour which is unwarranted in this example. Apr 3, 2022 at 17:54
  • 1
    Thanks for letting the readers know. I was not aware. And also useful for myself, although the chance I will need magick again in the coming years is rather thin.
    – Massimo
    Apr 4, 2022 at 19:33
0
  • 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
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy