How to crop an image using ImageMagick from the command line?
Solution 1
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
Solution 2
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.
Solution 3
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%
Related videos on Youtube
Dave M
20 years in systems admin and support roles in Windows environments
Updated on September 18, 2022Comments
-
Dave M over 1 year
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.
-
Leo Sanchez over 5 yearsRemove the zero (as in
1x20
) to actually remove anything -
Matthias Braun over 3 yearsIf 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 onconvert
andmogrify
. -
kojow7 over 3 yearsWhat is the purpose of the 0 for in the first place?
-
Mark Setchell over 3 years@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. -
Mark Setchell about 2 yearsThis is actually incorrect. Version 7 ImageMagick should almost never be used in the format
magick convert ...
. You should just usemagick INPUTIMAGE .... OPERATIONS ... OUTPUTIMAGE
unless you specifically want old Version 6 behaviour which is unwarranted in this example. -
Massimo about 2 yearsThanks 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.
-
Admin about 2 yearsSo, just to be clear, the syntax is
width x height + originX + originY
?