Search code examples
imagemagickpngcentercropgravity

Imagemagick crop doesn't seem to be respecting gravity center


bulk cropping PNG images which were previously rotated with Imagemagick using

convert thistle-7gn.png -rotate 75 thistle-7gn-75.png

this command

magick thistle-7gn-75.png -gravity center -crop 1600x1600+0+0 +repage thistle-7gn-75-cropped.png;

[EDIT: images appear to be converted from PNG to JPG when I upload] [EDIT 2: Imgur converts large PNG to JPG intentionally—links to original PNG] https://spinhead.com/thistle-7gn-75.png https://spinhead.com/thistle-7gn-75-cropped.png

with the attached image results in the off-center crop, also attached.

my understanding of the command is to use 'magick' rather than 'convert', then filename, set the origin to center rather than top left using '-gravity center', crop to size X by Y plus offset of X-direction Y-direction FROM THE CENTER OF THE ORIGINAL IMAGE, output new filename

I have successfully done exactly this process multiple times in the past months and thought I had crop with gravity sussed, but despite using what appears to be the same syntax with different images, it's not behaving as I expect.

Mac Mini Monterey, ImageMagick 7.1.1-15 Q16-HDRI x86_64 21298


Solution

  • Your input PNG, which I was able to download from thistle-7gn-75.png has a virtual canvas offset, which I can see from identify -verbose thistle-7gn-75.png as Page geometry: 4374x4374-587-587.

    That means you need to remove the virtual canvas offset using +repage right after reading the input PNG.

    So the following now works fine in Imagemagick for me.

    Input:

    enter image description here

    magick thistle-7gn-75.png +repage -gravity center -crop 1600x1600+0+0 +repage thistle-7gn-75-cropped.png
    

    Result:

    enter image description here