Skip to content

Add a command-line interface with output, OCR and display options - #13

Merged
groverkds merged 2 commits into
masterfrom
cli
Oct 11, 2026
Merged

groverkds merged 2 commits into
masterfrom
cli

Conversation

@groverkds

Copy link
Copy Markdown
Owner

Summary

Before this change, main.py always ran OCR, printed the text and opened GUI windows. It had no way to save the scan, and a missing file crashed it with an unhelpful error.

The CLI now lives in document_scanner/cli.py as main(argv), and main.py is a thin wrapper. This lets the packaging PR point an installed command straight at it.

python3 main.py receipt.jpg                 # saves receipt_scanned.png next to the photo
python3 main.py page.jpg -o scan.png --ocr  # custom output path, print the text
python3 main.py *.jpg --ocr --no-save       # text only, several photos
Option Effect
IMAGE ... One or more photos (--image still works for compatibility)
-o/--output PATH Where to save the scan (single image only); default <image>_scanned.png
--no-save Don't save the scan
--no-threshold Keep the scan in color
--no-rotate Skip auto-rotation
--ocr Print the page text, with ==> file <== headers for several photos
--show Show the photo and the scan in windows

Behaviour changes

  • OCR and windows are now opt-in. By default the scan is saved instead.
  • Exit status: 0 on success, 1 if any photo can't be read or saved (the others are still scanned), 2 for invalid arguments.
  • Without Tesseract: auto-rotation is skipped with a warning, and --ocr is an error.
  • No display: --show warns instead of letting OpenCV's Qt backend abort the process (exit 134). I reproduced that abort on a machine without a display.
  • .gitignore: ignores *_scanned.png, so scans of the sample photos aren't committed by accident.

Tests

tests/test_cli.py adds 16 tests. They cover:

  • default output naming and the black-and-white result
  • -o with --no-threshold
  • the old --image option
  • --no-rotate and --no-save
  • OCR output for one and several images
  • unreadable and missing files
  • the "page not found" fallback
  • invalid argument combinations
  • running without Tesseract
  • --show with and without a display
  • an end-to-end run of main.py on the sample receipt, checking OCR text and upright output

Tesseract and the GUI are mocked except in the end-to-end test.

Test plan

  • python -m pytest: 94 passed on Python 3.14 and 3.11.
  • Ran each option by hand on the sample photos, including a missing file, a corrupt file and --show without a display.
  • GitHub Actions run on this PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_013dqF6Adef93KhGwqLnxKPQ


Generated by Claude Code

main.py always ran OCR, printed the text and opened GUI windows, with
no way to save the scan, and crashed with an unhelpful error for a
missing file.

The CLI now lives in document_scanner/cli.py (main.py is a thin
wrapper):
- Takes one or more images as positional arguments (--image still works).
- Saves each scan as <image>_scanned.png by default, or to -o/--output.
- --no-save, --no-threshold, --no-rotate, --ocr and --show options.
  OCR and windows are now opt-in.
- Unreadable or missing images are reported and the rest still scanned;
  exit status 1 if any image failed, 2 for invalid arguments.
- Without Tesseract, auto-rotation is skipped with a warning and --ocr
  is an error. --show warns instead of aborting when there is no display.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013dqF6Adef93KhGwqLnxKPQ
@groverkds
groverkds merged commit eddb655 into master Oct 11, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants