Page 1 of 1

Crop geometry "center-north"

Posted: 2016-11-18T08:44:41-07:00
by baseballfury
Hi,

I would like to batch convert images with different oriantations and resolutions. I want to resize and crop them to a square images with the same resolution.

Code: Select all

convert intput -resize "435x435^" -gravity center -crop 435x435+0+0 +repage output
I ran this code on the files, which gave me the expected results: a square image cropped around the center.
This is perfect for most pictures, but some images show a single person in portrait-format, where half the head gets cut off.

So I am looking for a mix of "geometry center" and "geometry north".

Image


I have been using this php-function in the past to get the desired crop.

Code: Select all

//Portrait
$crop_y=($height_orig-$height_new)/6 //("/2" would center the image, "/6" turned out to be the optimal compromise)
imagecopyresampled($dst_img, $src_img, 0, 0, 0, $crop_y, 0, 435, 435, $width_orig, $height_orig)
Can I use Imagemagick alone to achieve this?

Re: Crop geometry "center-north"

Posted: 2016-11-18T09:05:53-07:00
by snibgo
Yes. You can either ask for "-gravity North" and start the crop somewhat lower, or "-gravity Center" and start the crop somewhat higher.

With IM v7, the arguments to "-crop" can be expressions, eg height/6. With IM v6, "-crop" can't use expressions, but viewports can.

Re: Crop geometry "center-north"

Posted: 2016-11-18T10:09:08-07:00
by GeeMack
baseballfury wrote:So I am looking for a mix of "geometry center" and "geometry north". [...] Can I use Imagemagick alone to achieve this?
In concept, yes. There are several ways to approach this problem. As snibgo already mentioned, ImageMagic 7 can do all kinds of calculations within the command itself. But if you can't migrate/upgrade and you're using IM6, you can still use FX expressions, just in more limited ways. Here's a way to remove a calculated amount from the top of the image before cropping it to square...

Code: Select all

convert input.jpg -set page "+0-%[fx:h*0.166]" -crop +0+0 +repage ...
That would leave the lower 5/6 of the image to manipulate with the rest of your "-gravity" and "-crop" operations. Just replace the "..." with whatever you need to do from there.

Re: Crop geometry "center-north"

Posted: 2016-11-20T03:03:58-07:00
by baseballfury
Thank you. I will upgrade to ImageMagick 7.