Page 1 of 1

SOLVED: Commands for Batch Cropping

Posted: 2015-11-25T09:17:16-07:00
by gregorissimo
Hello! I have thousands of PNG files where most of the image is transparent (defined by an alpha layer) and an object is somewhere within the image.

To crop these PNG files, I’m looking for a series of three commands that crops each side of the image according to certain rules (detailed below) and writes the image to the existing PNG file (or to a new PNG file with the same name, if that’s easier). Aside from the cropping, everything else in the file should be preserved (e.g. alpha layer).

The commands would need to work in batch across a a large number of files, and we’d be running them from the Windows command line. (They don’t need to use a particular version of ImageMagick or hook into any other platforms.)

I’d pay $50 for these commands.

Thanks!
Greg

=

The cropping rules:

1. Bottom Crop. Given a list of PNG files (not necessarily of the same height), crop the same amount off the bottom of each image where that amount is the minimum margin between the bottom of the object and the bottom of the image across all the files.

In algorithm form:
  • Determine the maximum amount that could be cropped off the bottom of each image (without cropping the object at all), and take the minimum of those maximums. Let’s call this max_crop.
  • Crop max_crop off the bottom of each of the PNG files.
Example:
  • If sample01.png has a distance of 15 px between the bottom of the object and the bottom of the image and sample02.png has a distance of 25 px between the bottom of the object and the bottom of the image, you’d crop 15 px off the bottom of both sample01.png and sample02.png.
2. Top Crop. Given a list of PNG files (not necessarily of the same height), crop the top of each image to the top-most point of the object within that image.

In algorithm form:
  • For each image, determine the maximum amount that could be cropped off the top (without cropping the object at all).
  • Crop that amount off the top of that image.
Example:
  • If sample01.png has a distance of 15 px between the top of the object and the top of the image and sample02.png has a distance of 25 px between the top of the object and the top of the image, you’d crop 15 px off the top of sample01.png and 25 px off the top of sample02.png.
3. Left and Right Crop. Given a list of PNG files (not necessarily of the same height), crop the same amount off the left and right side of each image where the amount cropped is the smallest of (a) the smallest distance between the left side of the image and the object’s left-most point across all the files, and (b) the smallest distance between the right ride of the image and the object’s right-most point across all the files.

In algorithm form:
  • Determine the maximum amount that could be cropped off the left side of each image (without cropping the object at all), and take the minimum of those maximums. Let’s call this left_max_crop.
  • Determine the maximum amount that could be cropped off the right side of each image (without cropping the object at all), and take the minimum of those maximums. Let’s call this right_max_crop.
  • Take the minimum of left_max_crop and right_max_crop. Let’s call this max_crop.
  • Crop max_crop off the left and right side of each of the PNG files.
Example:
  • If sample01.png has an object that terminates 15 px from the left side of the image and 20 px from the right side, and sample02.png has an object that terminates 25 px from the left side of the image and 10 px from the right side, you’d crop 10 px off both the right and left sides of both sample01.png and sample02.png.

Re: SOLVED: Commands for Batch Cropping

Posted: 2015-12-12T07:55:42-07:00
by gregorissimo
I figured out how to do this by using a combination of the following:

* One-sided trims: http://www.imagemagick.org/Usage/crop/#trim_oneside
* Width and height identification: http://blog.viktorkelemen.com/2011/06/i ... ck-in.html
* Chopping: http://www.imagemagick.org/Usage/crop/#chop