Skip to main content

Document image orientation detection and correction using projection profile analysis and optional Tesseract OSD.

Project description

docorient

Document image orientation detection and correction.

Detects and fixes rotation (0°, 90°, 180°, 270°) in scanned document images using projection profile analysis and optional Tesseract OSD.

Installation

pip install docorient

For 180° detection via Tesseract OSD:

pip install docorient[ocr]

Note: The [ocr] extra requires Tesseract installed on your system.

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.method)    # detection method used
print(result.reliable)  # confidence flag

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 multi-page document (majority voting)

from docorient import correct_document_pages

pages = [Image.open(f"page_{i}.jpg") for i in range(5)]
corrected_pages = correct_document_pages(pages)

Batch process a directory

Note (macOS/Windows): process_directory uses multiprocessing internally. Always call it inside if __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}")

CLI

docorient ./scans --output ./fixed --workers 4
docorient ./scans --dry-run
docorient ./scans --no-ocr --limit 100

How It Works

  1. Projection profile analysis detects 90° and 270° rotations by comparing horizontal vs vertical text energy
  2. Tesseract OSD (optional) detects 180° rotation with confidence thresholding
  3. Majority voting across pages of the same document improves reliability

Supported Formats

Any format readable by Pillow: JPEG, PNG, TIFF, BMP, GIF, WebP, and more.

Configuration

from docorient import OrientationConfig

config = OrientationConfig(
    osd_confidence_threshold=2.0,
    output_quality=92,
    max_osd_dimension=1200,
    projection_target_dimension=800,
    workers=4,
    resume_enabled=True,
)

License

MIT

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

docorient-0.1.1.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

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

docorient-0.1.1-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: docorient-0.1.1.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.11

File hashes

Hashes for docorient-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c34dceede0a2c57386eb80a78ef92ef797e13f6b6c7ca0662e53d37cdd1556ba
MD5 40b21af6d50dfaa561f98e762cde8487
BLAKE2b-256 d708617f6e65e824369bdb5ddccff04ee35b16fd97b28f5ac3f5480ada9c8db3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: docorient-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.11

File hashes

Hashes for docorient-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c4272d324f52a5068e7bfb31c53bc6f7bbed0806cfe602ad4b7c1939335bdb2d
MD5 7c307b399737cabc66f006d9de7323df
BLAKE2b-256 a262de8725ac90048d24b44ef93614a80c09df7b24e0a782fe19946c3ee518fa

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