Search code examples
image-processingimagemagickcropimagemagick-convert

How do I crop using relative offsets in ImageMagick?


For example, this does not work:

convert -crop '69%x27%+3%+13%' +repage source.jpg cropped.jpg

It does not result in an error, but the offset is ignored. Apparently the offset in a geometry can only be in pixels?

How do I achieve the desired effect?


Solution

  • You can do it like this:

    convert -crop $(convert source.jpg -format "%[fx:int(w*0.69)]x%[fx:int(h*0.27)]+%[fx:int(0.03*w)]+%[fx:int(0.13*h)]"  info:) +repage source.jpg cropped.jpg
    

    Try running the section inside the $() on its own to see how that generates what you need for the outer command...

    convert source.jpg -format "%[fx:int(w*0.69)]x%[fx:int(h*0.27)]+%[fx:int(0.03*w)]+%[fx:int(0.13*h)]"  info:
    

    If I run that part of the command on an image 100x100 pixels, I get this

    69x27+3+13