docorient
Document image orientation detection and correction.
Automatically detects and fixes rotation (0°, 90°, 180°, 270°) in scanned document images using a two-stage pipeline: energy-based axis detection + neural network flip classification. Achieves 98% accuracy across all four orientations. Includes multi-page majority voting and parallel batch processing.
No external binary dependencies — the neural model ships embedded in the package (~8.5 MB).
Installation
pip install docorient
Quick Start
Detect orientation
from PIL import Image
from docorient import detect_orientation
image = Image.open("document.jpg")
result = detect_orientation(image)
print(result.angle) # 0, 90, 180, or 270
print(result.reliable) # True
Correct a single image
from docorient import correct_image
corrected = correct_image(image)
corrected.save("fixed.jpg")
Correct with metadata
from docorient import correct_image
result = correct_image(image, return_metadata=True)
print(result.orientation.angle)
result.image.save("fixed.jpg")
Correct a multi-page document (majority voting)
from docorient import correct_document_pages
from PIL import Image
pages = [Image.open(f"page_{i}.jpg") for i in range(5)]
results = correct_document_pages(pages)
for i, result in enumerate(results):
result.image.save(f"fixed_{i}.jpg")
Batch process a directory
macOS / Windows:
process_directoryuses multiprocessing internally. Always call it insideif __name__ == "__main__":when running as a script.
from docorient import process_directory, OrientationConfig
if __name__ == "__main__":
config = OrientationConfig(workers=4, output_quality=95)
summary = process_directory("./scans", output_dir="./fixed", config=config)
print(f"Corrected: {summary.corrected}/{summary.total_pages}")
print(f"Errors: {summary.errors}")
CLI
docorient ./scans --output ./fixed
docorient ./scans --output ./fixed --workers 4 --quality 95
docorient ./scans --limit 100
docorient ./scans --dry-run
docorient --version
How It Works
DocOrient uses a two-stage detection pipeline:
-
PrimaryEngine — Analyzes horizontal and vertical energy distribution to determine if text is horizontal (0°/180°) or vertical (90°/270°). Fast, pure NumPy, ~97% accurate for axis detection.
-
FlipClassifierEngine — A MobileNetV2-based neural network (exported as ONNX, ~8.5 MB) that classifies whether a document is upside-down (180°). Trained on 3,000+ diverse document images from 5 public datasets (forms, invoices, receipts, technical documents, varied layouts). Achieves 96.8% standalone accuracy. Runs via ONNX Runtime at ~5ms per image on CPU.
The pipeline combines both: PrimaryEngine detects the axis, FlipClassifier resolves the ambiguity within each axis pair (0 vs 180, 90 vs 270). Combined pipeline accuracy: 98% across all four orientations (~20ms per image).
Configuration Reference
from docorient import OrientationConfig
config = OrientationConfig(
flip_confidence_threshold=0.50, # CNN confidence threshold (default: 0.50)
output_quality=92, # JPEG output quality 1-100 (default: 92)
primary_max_dimension=800, # Max image size for primary engine (default: 800)
workers=4, # Parallel workers; None = cpu_count-2 (default: None)
resume_enabled=True, # Resume interrupted batch jobs (default: True)
supported_extensions=( # File extensions to process
".jpg", ".jpeg", ".png",
".tiff", ".tif", ".bmp",
".gif", ".webp",
),
)
API Reference
detect_orientation(image, config=None) -> OrientationResult
Detects the orientation of a document image without modifying it.
| Parameter | Type | Description |
|---|---|---|
image |
PIL.Image.Image |
Image to analyze |
config |
OrientationConfig | None |
Configuration (uses defaults if None) |
Returns OrientationResult:
| Field | Type | Description |
|---|---|---|
angle |
int |
Detected angle: 0, 90, 180, or 270 |
method |
str |
Internal detection trace |
reliable |
bool |
Whether the detection is considered reliable |
correct_image(image, *, config=None, return_metadata=False)
Detects and corrects the orientation of a single image.
return_metadata=False-> returnsPIL.Image.Imagereturn_metadata=True-> returnsCorrectionResult
CorrectionResult fields: image: PIL.Image.Image, orientation: OrientationResult
correct_document_pages(pages, *, config=None) -> list[CorrectionResult]
Corrects orientation for all pages of a multi-page document with majority voting.
process_directory(input_dir, *, output_dir=None, config=None, limit=0, show_progress=True) -> BatchSummary
Processes all images in a directory in parallel.
BatchSummary fields:
| Field | Type | Description |
|---|---|---|
input_directory |
str |
Resolved input path |
output_directory |
str |
Resolved output path |
total_files |
int |
Number of source documents |
total_pages |
int |
Total images processed |
already_correct |
int |
Pages at 0° (no correction needed) |
corrected |
int |
Pages that were rotated |
corrected_by_majority |
int |
Pages corrected via majority voting |
errors |
int |
Pages that failed |
pages |
tuple[PageResult, ...] |
Per-page results |
Multi-page Document Grouping
process_directory automatically groups images by source document using the filename pattern <name>_p<number>.<ext>:
contrato_p1.jpg ┐
contrato_p2.jpg ├── grouped as "contrato" -> majority voting applied
contrato_p3.jpg ┘
edital_p1.jpg ┐
edital_p2.jpg ┘── grouped as "edital"
Files that don't match the pattern are treated as single-page documents.
Supported Formats
Any format readable by Pillow: JPEG, PNG, TIFF, BMP, GIF, WebP, and more.
Resume Support
Long batch jobs can be safely interrupted with Ctrl+C. Progress is saved to a _orientation_done.log file in the output directory. Re-running resumes from where it stopped.
To disable: OrientationConfig(resume_enabled=False)
Exceptions
from docorient import (
DocorientError,
DetectionError,
CorrectionError,
BatchProcessingError,
)
Development
git clone https://github.com/lucasleirbag/DocOrient.git
cd DocOrient
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest tests/ -v
ruff check src/ tests/
License
MIT — see LICENSE
Metadata
Release files for docorient 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| docorient-0.4.1.tar.gz | 8.2 MB | Without provenance | Details for docorient-0.4.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| docorient-0.4.1-py3-none-any.whl | Python 3 | none | any | Details for docorient-0.4.1-py3-none-any.whl |
Total release size: 16.5 MB
Release files / docorient-0.4.1.tar.gz
| Download URL | docorient-0.4.1.tar.gz |
|---|---|
| Size | 8.2 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0f269a98e203821f0488f6e5deaf59ccd2e591b13dc579fb6d3e92c2343a85f0
|
|
BLAKE2b-256 checksum How to use checksums |
70eac146d47c4d678f5c6edeb76504d00e7b0189bb9012fd5cb4c57446a5de30
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.11
|
Release files / docorient-0.4.1-py3-none-any.whl
| Download URL | docorient-0.4.1-py3-none-any.whl |
|---|---|
| Size | 8.2 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d4b290721d9ceed091ddb1b2551875727058dfd9c00ace630eab137993b55719
|
|
BLAKE2b-256 checksum How to use checksums |
66ccf99834306e4864c32346c33e04e2d70548bef58d092218ecfd85408f610f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.11
|
Security for docorient 0.4.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.