Skip to main content

A tool to detect and crop multiple photos from scanned images

Project description

Image Cropper

Tests PyPI version Code style: black

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

  1. Make sure you have Poetry installed (Poetry Installation Guide)

  2. Clone this repository:

git clone https://github.com/yourusername/image-cropper.git
cd image-cropper
  1. 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: Example Test Image

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: Example Cropped Image 1 Example Cropped Image 2 Example Cropped Image 3 Example Cropped Image 4

Expected Cropping Behavior

When processing these test images, the cropping script should:

  1. Detect the boundaries between the white background and colored regions
  2. Create separate image files for each detected region
  3. Remove the white borders around each photo
  4. Preserve the original aspect ratio of each photo
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

crop_scanned_photos-0.1.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

crop_scanned_photos-0.1.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file crop_scanned_photos-0.1.1.tar.gz.

File metadata

  • Download URL: crop_scanned_photos-0.1.1.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

Hashes for crop_scanned_photos-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8a5cc2ae4c549601419a7fa05d2be6e43d7fa85aee73e77d4fdb4943fab99c13
MD5 4b93ebf6482f1a47739fd7757ac2055f
BLAKE2b-256 10c60d54b9edccfb7988ebe7325e2264ab6dcbbbfee68a9fa6fc955e4edff797

See more details on using hashes here.

File details

Details for the file crop_scanned_photos-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: crop_scanned_photos-0.1.1-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

Hashes for crop_scanned_photos-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a52002093e42e3392bfa66e5db785f891a8efbdad0fbff1329a88ff2bd70c473
MD5 f0b90b531469c8e36d566c9effe7cf18
BLAKE2b-256 12f0c885f7b69b917f525d0698f69fe396a4ccd1724b3574b45f156c4b5e8876

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page