Page 1 of 1

Crop several images to largest width and height amongst them

Posted: 2016-03-25T01:11:28-07:00
by Daedolon
Hi,

I have several PNGs, all of them of the same dimensions and I'm trying to figure out how to use Imagemagick to crop them automatically based on the widest and highest dimensions filled.

For example, I have 3 images of 30x30.
First one contains a rectangle 20x10 centered
Second one contains a rectangle 10x20 centered
Third one contains a square 10x10 centered

The result would be 3 images 20x20 cropped and centered.

I read the Imagemagick doc and can crop one image but I don't know how to do all images at once and depending on some parameters like my case.

Thanks !

Re: Crop several images to largest width and height amongst them

Posted: 2016-03-25T01:46:29-07:00
by snibgo
When you have a 30x30 image, how do you determine that it has a rectangle 20x10 centred? Example images might help.

You probably need a script. For the cropping, if the job is small, I would have a shell loop with one convert per image, using environment variables. But you might use mogrify, or an ImageMagick script.

EDIT: Perhaps you want to apply the least-aggressive trim that is common to a bunch of files. If so, I have published a Windows BAT script for exactly that job. It could be easily translated to bash etc. See frTrim.bat on my "Fractal noise animations" page.

Re: Crop several images to largest width and height amongst them

Posted: 2016-03-25T03:33:43-07:00
by Daedolon
Well actually, it doesn't matter if the rectangle is centered on not. Reading my explanation, I wasn't clear indeed.

What I need to do is basically the same thing as the "Autocrop" button from Gimp : I want to remove all the border transparency from several PNGs but the crop needs to be based on the widest and highest image, knowing that the widest and highest can be from different image. In my example, widest is the first rectangle and highest is second rectangle, so basically, if the rectangle are indeed centered, the script would need to crop 5px top, 5 px bottom, 5px left and 5px right. But it could happen the first rectangle is not centered and is instead 2px on the left from the center. in such case, the script would then crop 5px, 5px bottom, 3px left and 7px right.

Following your first answer, it seems like Imagemagick can't do all of it alone so I'll try to get a PHP script to find the top-left and bottom-right coordinate and then use imagemagick based on these coordinates. But if you have an easier way to do it with Imagemagick, I'm all ears !

Re: Crop several images to largest width and height amongst them

Posted: 2016-03-25T03:48:37-07:00
by snibgo
It sounds as if "-format %@" gives you the information you need from each image, and some messy processing can calculate the least-aggressive trim.

But there is a much simpler way. Composite the images over each other ("-layers merge"). Then "-format %@" of this composite gives the required crop.

Re: Crop several images to largest width and height amongst them

Posted: 2016-03-25T06:28:11-07:00
by Daedolon
Yes, the composite gives the required crop indeed. Would you have a sample of the command to run for the example by any chance ? I fear I'll need 1 or 2 hours to figure out how to merge all those commands :x

Re: Crop several images to largest width and height amongst them

Posted: 2016-03-25T06:40:47-07:00
by snibgo
It's much easier if you can supply example images. Then I can check the commands do what I think you want.

It should be something like this:

Code: Select all

convert A.png B.png C.png -layers merge -format %@ info:
[EDIT: correction: I typed B.png twice instead of C.png.]

This gives the crop string. Read that into an environment variable. The mechanics of that will depend on what shell you use.

Code: Select all

convert A.png -crop %CROP_STRING% +repage A_cropped.png
... where %CROP_STRING% is the environment variable. Repeat for the other images.

I don't use PHP, and can't advise on that.

Re: Crop several images to largest width and height amongst them

Posted: 2016-03-25T08:03:54-07:00
by Daedolon
For the reference images, I'm sorry, I don't know how to upload them. But your sample code already puts me on the right direction so I should be able to figure out the rest.

Thanks a lot for your help ! I'll let you know once I have the complete process figured out.

Re: Crop several images to largest width and height amongst them

Posted: 2016-03-25T08:12:34-07:00
by snibgo
I've made a minor correction to my previous post.

You can upload images to a free site such as dropbox.com and paste URLs here.