Batch Image auto crop, apply fixed width, and maintain aspect ratio

3

I'm finding it hard to find a solution to what I'm trying to do.

Simply put, the usage of these images is for an online store.

The IrfanView batch processing using the autio crop borders basically sorts out the cropping. I need to cut out the white/black or whatever uniform border exists in the images being processed.

But I need to keep the image in a perfect square. Like 300x300 pixels.

So, for example, if the image being cropped has a lengthy focus, it would crop the white space / margin until it reaches the top and bottom. it would then only crop the left and right to a point where the size of the left and right equals the length. Either by removing only a partial amount of white-space (margin) - or if need be, add some white-space (margin).

StuyvesantBlue

Posted 2016-01-01T01:07:19.430

Reputation: 41

1To add further, with IrfanView doing a batch conversion, first doing Auto crop borders, and then doing a resize, gives me a result that I could work with. But doing both in one conversion does not. Is there a way to get IrfanView to first do the Auto crop and then perform the resize all in one go, instead of doing the conversions separately? – StuyvesantBlue – 2016-01-01T02:31:10.200

1Oh gosh, I see now that in the bottom right corner of the Advanced settings for batch processing in IrfanView has an option to adjust processing order. – StuyvesantBlue – 2016-01-01T02:34:26.387

If you solve your issue post the solution as an answer... it can be useful to others or improved by others. :) – Hastur – 2016-01-01T13:48:51.830

1@Hastur Understood. Didn't exactly solve the issue, but gives me something I can work with... – StuyvesantBlue – 2016-01-02T14:00:49.117

Answers

3

imagemagick has the marvelous option -trim. I remember using it along with -size about a year ago on a bunch of 4500 pictures to remove the empty monochrome border and keep just the product. I remember putting all images in directory a and creating an empty dir b; then from dir a using something like

for i in *; do convert-im6.q16 $i -trim -resize 300 ../b/$i; done

Then I waited about an hour, and I had 4500 images in dir b with the monochrome background trimmed and width 300 pixels. The convert-im6.q16 command may be named a bit differently on other systems, like convert or something.

gmelis

Posted 2016-01-01T01:07:19.430

Reputation: 473

1

I used IrfanView to accomplish a workable solution.

Using the Batch Conversion/Rename from File >> I simply add images to the Input Files dialog.

  1. Use advanced options in Batch conversion settings, then click Advanced.

  2. Tick RESIZE and select SET NEW SIZE then SET ONE OR BOTH SIDES TO. In the height/width put the same size (I use 300px). What this will do is resize the image to a maximum point of that size. So it will take the biggest size (depending on whether the image is portrait or landscape) - and it will make that 300px. And the other side will be scaled proportionally provided you maintain aspect ratio.

  3. What you also want to do is Smart Crop, in the case of IrfanView it's called Auto Crop Borders - you'll see this setting to the right of the RESIZE dialog.

Before you complete that, you want to make sure that it first does the cropping, and then the resizing, otherwise you'll end up with something smaller than the 300px, because if it first resizes to 300px and then crops after that...you can imagine the outcome.

  1. So in the bottom right corner, tick Custom Processing Order, and click Change Order. Move the Auto crop borders to the top. Click OK, click OK again, and you're ready to convert. Clik Start Batch.

This didn't exactly solve my exact question, but it doesn't matter so long as I was able to get biggest side to 300px, and smart cropping before the resize. The rest I can accomplish with CSS in my html, which may actually be a better solution.

StuyvesantBlue

Posted 2016-01-01T01:07:19.430

Reputation: 41

IMHO it is better if you find a batch solution, maybe with imagemagick. The steps should be 1 autocrop 2 find bigger side 3 scale without deformation to 300 for the bigger side, 4 sum centered to a white square 300x300. If you wat a border of 20px scale to 280. Imagemagick will be you best friend. Linux should be a good environment for the script... good luck. – Hastur – 2016-01-02T22:17:33.410

1

Assuming your original images don't have an actual black or white border and you just want to avoid the creation of such, you can use Imagemagick's convert:

convert in.jpg -geometry 300x300^ -gravity center -crop 300x300+0+0 out.jpg

The caret (^) makes sure the image fills both width and height.

However only using the -geometry parameter would leave you with an image with one of the sides too large, assuming you don't have square source material. So you need to crop around the center to get the final image resolution. +0+0 ensures you don't get the cut-off parts as separate files, since you don't need them.

If you actually have borders in the source image, you might want to try using -trim right after in.jpg.

qubodup

Posted 2016-01-01T01:07:19.430

Reputation: 3 736

0

My requirements were slightly different in that I was wanting to trim transparent pixels, but it may be functionally the same as removing a border? Maybe.

None of these answers worked for me. After placing the bounty, I kept searching and found that extent was the missing piece.

magick convert *.png -trim -background none -resize 300x300 -gravity center -extent 300x300 out/processed.png

Here is the breakdown of what each parameter does, as far as I understand it:

  • -trim: Trim all transparent pixels (left, right, top, bottom)
  • -background none: maintain transparency for resize (order is important!)
  • -resize 300x300: Makes the maximum width and height 300. Does not make it square.
  • -gravity center: Centers the image (may not be necessary, may work with both resize and extent, didn't test much).
  • -extent 300x300: Resizes the canvas, this is what makes it square again.

Which translates to basically 3 steps:

  1. Remove transparent edges
  2. Resize so that the width/height is no more than 300 pixels.
  3. Make both the width and height 300 pixels.

Devil's Advocate

Posted 2016-01-01T01:07:19.430

Reputation: 1 397