Search code examples
positionimagemagickcropimagemagick-convertgravity

Imagemagick crop from northeast


I am trying to crop an image starting from top-right and cut out a 48x48 box. This is the image I'm working with

enter image description here

I've tried this

in.png -gravity northeast -crop 48x48 out.png

out-0.png enter image description here out-1.png enter image description here out-2.pngenter image description here out-3.png enter image description here

Which creates like 4 files, none of which are what I want. When I add x and y values (which I don't want to), it crops from the northeast correctly with only 1 output image, but the box is not 48x48, its 46x38

in.png -gravity northeast -crop 48x48+0+0 out.png

out.png
enter image description here

This gives different outputs for different images. I just tried another and ended up with a 33x48 output.

I need to use the gravity setting instead of the x and y offsets because I'm batch processing a lot of images that are different sizes.

This is the desired output

enter image description here

Can someone please explain to me what I'm doing wrong? Thanks!


Solution

  • If your input image has paging information, the result of your crop may not be what you expect. When working with unknown images, you might do a "+repage" right after reading in the image. Also, when you "-trim" an image, the paging information from the original image remains. The "-crop" will use that paging information instead of the actual height and width, so "+repage" after a "-trim" unless you know you'll need that information. Try this...

    convert inimage.png -trim +repage -gravity northeast -crop 48x48+0+0 outimage.png
    

    You should also use "+repage" after any crops if you intend to continue processing the images.