A tool to detect and crop multiple photos from scanned images
Project description
Image Cropper
A Python tool that processes scanned images to automatically detect and crop multiple photos. Specifically designed for scanned images with white borders (as commonly produced by scanners), where multiple photos were scanned together on a single page. The tool will identify each individual photo and save them as separate files.
Features
- Detects multiple photos in scanned images with white borders
- Automatically crops and saves individual photos
- Supports multi-threading for faster processing
- Configurable via command-line arguments or environment variables
- Supports various image formats (PNG, JPG, JPEG)
Installation
-
Make sure you have Poetry installed (Poetry Installation Guide)
-
Clone this repository:
git clone https://github.com/hspedro/crop-scanned-photos.git
cd crop-scanned-photos
- Install dependencies:
poetry install
Usage
Command-Line Arguments
poetry run python crop.py --help
Environment Variables
You can also configure the tool using environment variables:
Available arguments:
--input-folder: Input folder containing scanned images (default: "raw")--output-folder: Output folder for cropped images (default: "output_images")--threads: Number of processing threads (default: 1)--threshold-value: Threshold value for image processing (default: 240)--threshold-max: Maximum threshold value (default: 255)--min-contour-width: Minimum contour width to process (default: 50)--min-contour-height: Minimum contour height to process (default: 50)--allowed-extensions: Comma-separated list of allowed file extensions (default: .png,.jpg,.jpeg)
export INPUT_FOLDER="scans"
export OUTPUT_FOLDER="cropped"
export THREADS="4"
export THRESHOLD_VALUE="230"
export THRESHOLD_MAX="255"
export MIN_CONTOUR_WIDTH="100"
export MIN_CONTOUR_HEIGHT="100"
export ALLOWED_EXTENSIONS=".png,.jpg,.jpeg"
poetry run python crop.py
Note: Command-line arguments take precedence over environment variables.
Directory Structure
crop-scanned-photos/
├── raw/ # Default input directory for scanned images
├── output_images/ # Default output directory for cropped images
├── examples/ # Default output directory for test images
├── crop.py # Main script
├── create_test_image.py # Script to generate test images
├── pyproject.toml # Poetry configuration
└── README.md # This file
Test Images Generation
The project includes a script to generate test images that simulate scanned photos with white borders. These test images are useful for development and testing of the cropping functionality.
Generating Test Images
Use the create_test_image.py script to generate test images:
poetry run python create_test_image.py -n 4 -o test_4_photos.jpg -f examples
This will create a test image with 4 photos in the examples directory.
Running the Cropping Script on Test Images
To run the cropping script on the test images, use the following command:
poetry run python crop.py --input-folder examples --output-folder examples/cropped
This will process the test images in the examples directory and save the cropped images in the examples/cropped directory.
Options:
-n, --num_photos: Number of photos to include (default: 4)-o, --output: Output filename (default: test_N_scan.jpg where N is number of photos)-f, --folder: Output folder path (default: examples)
Test Image Structure
The generated test images have the following characteristics:
- A4 scan proportions (2000x2800 pixels)
- White background simulating scanner bed
- Colored rectangles representing photos (for easy visual testing)
- Automatic grid layout based on number of photos
- 100px white margins between photos
Example layout for 4 photos:
After running the cropping script, the output directory will contain the following files:
- test_1_scan.jpg (blue region)
- test_2_scan.jpg (red region)
- test_3_scan.jpg (green region)
- test_4_scan.jpg (yellow region)
Example output:
Expected Cropping Behavior
When processing these test images, the cropping script should:
- Detect the boundaries between the white background and colored regions
- Create separate image files for each detected region
- Remove the white borders around each photo
- Preserve the original aspect ratio of each photo
- Name the output files based on the input filename with sequential numbering
For example, processing test_4_scan.jpg should produce:
- test_4_scan_1.jpg (blue region)
- test_4_scan_2.jpg (red region)
- test_4_scan_3.jpg (green region)
- test_4_scan_4.jpg (yellow region)
Each output image should contain only the colored rectangle without any white borders.
Requirements
- Python 3.8 or higher
- OpenCV (installed automatically via Poetry)
License
MIT License
Copyright (c) 2025 Pedro Soares
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file crop_scanned_photos-0.1.2.tar.gz.
File metadata
- Download URL: crop_scanned_photos-0.1.2.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.12.3 Linux/6.8.0-1021-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dffb345f7c0e962d9c6379e4150a17c3b708f45ce66b9c8faaae82d54f82eab0
|
|
| MD5 |
05bdd6a30469f8895abc59e0ca446289
|
|
| BLAKE2b-256 |
8db569cea9eb89a986262b4b3284772da57eecd1f1c5ed3e56ca7a7f89d184f5
|
File details
Details for the file crop_scanned_photos-0.1.2-py3-none-any.whl.
File metadata
- Download URL: crop_scanned_photos-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.12.3 Linux/6.8.0-1021-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2c569c066c742b1ebf3ee527850b176881cb93e7bf4d73d1d3909f4fcbe4bfd
|
|
| MD5 |
9dee4211c7e0c7d0f537d4cabbf8d6b1
|
|
| BLAKE2b-256 |
3885b558f5d0031443b7e408aa91787d2c866f99902cf8acdb67b361864e54bb
|