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 !
Crop several images to largest width and height amongst them
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Crop several images to largest width and height amongst them
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.
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.
snibgo's IM pages: im.snibgo.com
Re: Crop several images to largest width and height amongst them
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 !
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 !
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Crop several images to largest width and height amongst them
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.
But there is a much simpler way. Composite the images over each other ("-layers merge"). Then "-format %@" of this composite gives the required crop.
snibgo's IM pages: im.snibgo.com
Re: Crop several images to largest width and height amongst them
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Crop several images to largest width and height amongst them
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:
[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.
... where %CROP_STRING% is the environment variable. Repeat for the other images.
I don't use PHP, and can't advise on that.
It should be something like this:
Code: Select all
convert A.png B.png C.png -layers merge -format %@ info:
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
I don't use PHP, and can't advise on that.
snibgo's IM pages: im.snibgo.com
Re: Crop several images to largest width and height amongst them
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.
Thanks a lot for your help ! I'll let you know once I have the complete process figured out.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Crop several images to largest width and height amongst them
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.
You can upload images to a free site such as dropbox.com and paste URLs here.
snibgo's IM pages: im.snibgo.com