ScanCropper is a Python script designed to identify, crop, and correctly orient photographs in scanned images (".jpg", ".jpeg", ".png", ".pdf", ".bmp"). It is particularly suited for finding rectangular images on a white background. It can handle multiple photos in a single scan and process multiple scanned images at once, meaning you don't have to perfectly align the photos in your scanner. Furthermore, there is the option to manually name and add metadata to each extracted image (an image preview is given).
PDF scans are converted into a png with a default dpi of 600 (can be changed in scan_cropper.py) before extracting the images.
Make sure that the pdf scans only have a length of one page.
Suppose you have a photo album and you scan 3 photos at a time, leaving some space between them on the scanner. For each file it processes, it will output 3 individual, cropped, and correctly oriented photos from the scan.
python3 scan_cropper.py --photos-per-scan 3 --dir ./scans --odir ./images --manual-name --manual-metadataExample Scan:
Result Image 0:
Result Image 1:
Result Image 2:
- Python 3.9 or above
- OpenCV
- NumPy
- PyMuPDF
- pillow
- py3exiv2
You can install the required Python packages with pip:
pip install opencv-python-headless numpy PyMuPDF pillowIn order to manually add metadata to each image metadata to the files when using --manual-metadata you also need:
pip install py3exiv2If you get an error when installing this package it is likely that certain dependencies are missing. On Ubuntu install them with:
sudo apt-get install build-essential python-all-dev libexiv2-dev libboost-python-devNavigate to the directory containing the ScanCropper script:
cd /path/to/ScanCropperRun the script, specifying the input directory (containing your scanned images) and the output directory (where the cropped images will be saved):
python scan_cropper.py --dir /path/to/input/dir --odir /path/to/output/dirYou can customize the processing with several options. Here are some, have a look to arg_parse.py for more and using environment variables:
`--dir` or `-d` Specify the location of the scans to process.
`--odir` or `-o` Specify where to save the output images.
`--pdir` or `-po` Specify where to move processed scans.
`--output-format` or `-of` Defines the image output format [jpg (default) or png].
`--output-jpeg-quality` Defines the quality of jpeg image output, 1-100.
`--watch` or `-w` Specify weather to watch input location for new scans and process them.
`--manual-name` or `-mn` Independently shows each image and asks for the image name before saving.
`--manual-metadata` or `-mm` Independently shows each image and asks for individual metadata to attach to the image (only possible for jpg as output format).
`--num-threads` or `-n` Number of threads to use (default: system number of cores).
`--thresh` or `-t` Threshold value for determining photo edges. Use higher values for brighter images, lower for tighter cropping (default: 230).
`--blur` or `-b` Amount of blur to apply when processing. Different values may affect how well scans are found and cropped. Must be an odd number greater than 1 (default: 9).
`--output-file-name-prefix` or `-p` Prefix string to append to the start of output image file names.
`--output-file-name-prefix-strftime` Prefix string, strftime formatted to get datetime.
This project is an up to date and extended version of https://github.com/murniox/ScanCropper, which is an up to date and extended version of https://github.com/kosenina/ScannedImageMultiCrop/tree/master.
Usefull links:
https://github.com/polm/ndl-crop
https://flothesof.github.io/removing-background-scikit-image.html
https://github.com/Claytorpedo/scan-cropper/



