Imagemagick convert is pretty handy tool to manipulate images and can be used to crop an image. This can be useful in automation and also doing bulk operation on images. We’ll be using the following image (convert-crop-img1.jpg width,height=480×320) for the purpose of this tutorial.
This tutorial is performed on Mac (OS X Yosemite) with Imagemagick version 6.9.2-7.
Crop image using target width,height and offset in pixel
Crop the image to target size 240×160 with crop starting point at x=100,y=50
$ convert -crop 240x160+100+50 convert-crop-img1.jpg convert-crop-img2.jpg
Here is the outcome image (convert-crop-img2.jpg):
Crop image using target width,height in percentage
Crop the image to target size 50%x50% with crop starting point at x=100,y=50
$ convert -crop 50%x50%+100+50 convert-crop-img1.jpg convert-crop-img3.jpg
Here is the outcome image (convert-crop-img3.jpg):
Crop image using both target width,height and offset in percentage
Crop the image to target size 50%x50% with crop starting point at x=25%,y=25%
$ OFFSETX=$(identify -format '%[fx:w/4]' convert-crop-img1.jpg) $ OFFSETY=$(identify -format '%[fx:h/4]' convert-crop-img1.jpg) $ echo $OFFSETX $OFFSETY 120 80 $ convert -crop 50%x50%+$OFFSETX+$OFFSETY convert-crop-img1.jpg convert-crop-img4.jpg
Here is the outcome image (convert-crop-img4.jpg):