How to crop/enlarge product photos white margin, in batch automatically?

TL;DR: I have created a drag and drop shell script that works on Mac/Linux, which can get the product photo in the right size you want or align with same white margin, with help from a few open source library from Rust .

https://gist.github.com/jimmyctk/94c072b18617315fe79a95f730f59dd3

Background

Have you ever been annoyed that some product photos in your catalog look bigger and some look smaller? Do you want them in the same size?

Example1Example2
Example product catalog on a ecommerce website

Deliverables

The processing of 100 1000x1000px images can be done in 96.1s.

./image-optimize.sh 1000 100  96.10s user 27.26s system 269% cpu 45.724 total
BeforeAfter

How it works

The code simply has 3 steps:

  1. Detect the bounding box of the object, crop it
  2. Resize (shrink/enlarge) the cropped image to targeted resolution
  3. Add the white border back to the image with desired width

Reference

https://crates.io/crates/auto-image-cropper

https://crates.io/crates/image-resizer

https://crates.io/crates/filmborders