Home (All Topics) → Technology → Software → AI-Created Software
Crops away the white or near-white border around scanned photos, a whole folder at a time, and can straighten prints that were scanned at a slight angle. Use it after scanning prints on a flatbed scanner, so each photo comes out as just the photo.

Needs Python 3.8 or later, with Pillow and NumPy:
pip install Pillow numpy
python auto-crop-photo-borders.py --input-folder-path Scans --output-folder-path Cropped
Each cropped photo is saved in the output folder under its original name and format. The script reads JPEG, PNG, TIFF, BMP and WEBP.
A pixel counts as border when each of its color channels is within the tolerance of the border color (white unless you give another). The script finds the area that isn’t border, then keeps trimming rows and columns that are at least 80% border, so an off-white edge or a speck of dust doesn’t stop it. Raise --tolerance for borders that are gray or yellowed, or use --color-hex auto to take the border color from each scan’s edges.
With --deskew, the script measures how far each photo is tilted, up to 10 degrees, rotates it straight and then crops it. Here are two scans, one straight and one laid 1.4 degrees off:
python auto-crop-photo-borders.py --input-folder-path scans --output-folder-path out --deskew
Input folder : scans
Output folder : out
Border color : #FFFFFF → RGB(255, 255, 255)
Tolerance : 10/100 → ±26 per channel
Fill threshold: 80% of a row/col must match to be cropped
Options : deskew (±10°)
Quality : 95 Workers: 1
------------------------------------------------------------------------
[1/2] lake-straight.png OK cropped top=80px, bottom=250px, left=92px, right=288px
[2/2] lake-sunset-1987.jpg OK rotated -1.40°; 1880x1330 -> 1525x1026
------------------------------------------------------------------------
Done. 2 processed, 0 skipped, 0 errors.
With --split, each print found on a scan is cropped on its own and saved as NAME_1, NAME_2 and so on.
Objects smaller than 2% of the scan are ignored, and objects closer together than 1% of the scan’s shorter side are treated as one photo.
--recursive also processes subfolders and recreates them in the output folder. --workers 4 processes four scans at once (0 uses every CPU core); each worker needs memory for one full-size scan. --dry-run reports what would be cropped without writing anything, and --log-csv crops.csv records each file’s crop box, rotation and size.
Every option, as typed. Options without a value are switches that turn a feature on.
| Option | What it does |
|---|---|
--input-folder-path SCANS |
The folder of scans. Required. |
--output-folder-path CROPPED |
Where the cropped photos go. Required. |
--recursive |
Also processes subfolders, recreating them in the output folder. |
--color-hex FFFFFF |
The border color, or auto to detect it from each scan’s edges. Default: FFFFFF. |
--tolerance 10 |
How far each color channel may be from the border color, 0 (exact) to 100 (any color). Default: 10. |
--fill-threshold 80 |
How much of a row or column, in percent, must be border for it to be cropped. Default: 80. |
--denoise 3 |
Ignores dust specks up to about this many pixels across; 0 turns it off. Default: 3. |
--ignore-edge 0 |
Ignores this many pixels along every edge of the scan, such as a dark line from the scanner’s lid. Default: 0. |
--padding 0 |
Pixels of border to keep around the photo; a negative value crops further in. Default: 0. |
--max-crop 100 |
Never removes more than this percentage of the width or height from one side. Default: 100 (no limit). |
--deskew |
Straightens tilted photos. |
--max-skew 10 |
The largest tilt, in degrees, to look for. Default: 10. |
--min-skew 0.1 |
Photos tilted less than this many degrees aren’t rotated. Default: 0.1. |
--split |
Saves each photo on a scan as a separate file. |
--split-min-area 2 |
Ignores objects smaller than this percentage of the scan. Default: 2. |
--split-merge-distance 1 |
Treats objects closer than this percentage of the scan’s shorter side as one photo. Default: 1. |
--quality 95 |
JPEG and WEBP quality, 1 to 100, or keep to reuse the source JPEG’s compression (WEBP is then lossless). Default: 95. |
--skip-existing |
Skips scans whose output file already exists. |
--allow-overwrite |
Allows the output folder to be the input folder, replacing the originals. |
--workers 1 |
How many scans to process at once; 0 uses every CPU core. Default: 1. |
--dry-run |
Reports what would be cropped without writing any images. |
--log-csv crops.csv |
Writes a CSV log with the crop details of every scan. |
The script exits with 1 if any scan failed, otherwise 0.
--max-crop 15, or raise --fill-threshold.--ignore-edge 10 to skip it.--tolerance, or use --color-hex auto.
--max-skew degrees, not for photos turned sideways or upside down.
