Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

How to crop an image using ImageMagick from the command line?

Writer Sophia Terry

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

3 Answers

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

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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