Auto Crop Photo Borders

← All AI-created software

This is AI-generated (aka vibe-coded) software. It is specific to my needs and my system and may not work for you or yours. Support is limited, but if you report a bug I'll certainly look into it. The code is provided in the zip file for you to review should you choose.
⬇️ Download Auto Crop Photo Borders.zip540.0 KBLast Updated: November 14, 2026Python

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.

Before and after: a scanned print with a white border and a slight tilt, and the same photo cropped and straightened
On this page

Features

  • Removes white or off-white borders, such as the scanner bed or the print’s own edge, within a tolerance you set
  • Detects the border color from the edges of each scan, for scans on a colored background
  • Straightens photos scanned at a slight angle
  • Splits a scan holding several prints into one file per photo
  • Keeps EXIF, the color profile and DPI where the output format supports them
  • Works through folders and subfolders, on several CPU cores, with a CSV log of every crop

Usage

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.

Cropping borders

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.

Straightening tilted photos

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.

Several photos on one scan

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.

Large batches

--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.

Command line

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.

Troubleshooting

  • A photo with a bright sky or white edge is cropped too far: add --max-crop 15, or raise --fill-threshold.
  • A thin dark line along the scan’s edge stops the crop: add --ignore-edge 10 to skip it.
  • The border is left in place: it is too far from white. Raise --tolerance, or use --color-hex auto.
  • “Saved uncropped”: no border color was found along the edges, or the whole image matched it. Check the border color and tolerance.

Limitations

  • 16-bit TIFF and PNG scans are saved as 8-bit, because Pillow reads them that way; the script warns when it happens.
  • Straightening looks for tilts of up to --max-skew degrees, not for photos turned sideways or upside down.
⬇️ Download Auto Crop Photo Borders.zip540.0 KBLast Updated: November 14, 2026Python

← All AI-created software