Metadata-Version: 2.4
Name: clipocr
Version: 0.1.0
Summary: Cross-platform screenshot & clipboard OCR with bbox, powered by RapidOCR.
Project-URL: Homepage, https://github.com/lidongyangLeo/clipocr
Project-URL: Repository, https://github.com/lidongyangLeo/clipocr
Project-URL: Issues, https://github.com/lidongyangLeo/clipocr/issues
Project-URL: Changelog, https://github.com/lidongyangLeo/clipocr/blob/main/CHANGELOG.md
Author-email: Ethan Li <lidongyangLeo@users.noreply.github.com>
License: MIT License
        
        Copyright (c) 2026 Ethan Li (lidongyangLeo)
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: chinese-ocr,clipboard,image-to-text,ocr,rapidocr,screenshot
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Multimedia :: Graphics
Classifier: Topic :: Scientific/Engineering :: Image Recognition
Classifier: Topic :: Utilities
Requires-Python: >=3.8
Requires-Dist: numpy>=1.21
Requires-Dist: onnxruntime>=1.15
Requires-Dist: pillow>=9.0
Requires-Dist: rapidocr>=1.4.0
Provides-Extra: dev
Requires-Dist: build>=1.0; extra == 'dev'
Requires-Dist: pytest-cov>=4.0; extra == 'dev'
Requires-Dist: pytest-mock>=3.10; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: ruff>=0.1.0; extra == 'dev'
Requires-Dist: twine>=4.0; extra == 'dev'
Description-Content-Type: text/markdown

# clipocr

[![CI](https://github.com/lidongyangLeo/clipocr/actions/workflows/ci.yml/badge.svg)](https://github.com/lidongyangLeo/clipocr/actions/workflows/ci.yml)
[![PyPI version](https://img.shields.io/pypi/v/clipocr.svg)](https://pypi.org/project/clipocr/)
[![Python versions](https://img.shields.io/pypi/pyversions/clipocr.svg)](https://pypi.org/project/clipocr/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

> Cross-platform screenshot & clipboard OCR for Python. Powered by [RapidOCR](https://github.com/RapidAI/RapidOCR). Returns text plus bounding boxes. Works on **macOS / Windows / Linux**.

[中文文档](README.zh-CN.md)

## Why

You took a screenshot. You want the text out of it — without uploading it anywhere.

`clipocr` reads the image directly from your clipboard or a file, runs a fast on-device OCR model, and gives you back:

- the recognized text (in reading order),
- a list of `Block`s, each with `text`, `bbox` (xmin, ymin, xmax, ymax) and `confidence`,
- and overall metadata (engine, image size, average confidence).

Good for: error-screenshot to text, code screenshots, UI mock review, document snippets, anything where you don't want to send your image to a cloud API.

## Install

```bash
pip install clipocr
```

The first run downloads ~15 MB of ONNX models into your site-packages (one-time, then cached).

## Quick start

### Python

```python
from clipocr import ocr, ocr_clipboard

# From a file path
result = ocr("screenshot.png")
print(result.text)
for block in result.blocks:
    print(block.bbox, block.text, block.confidence)

# From the system clipboard
result = ocr_clipboard()
print(result.text)
```

`OcrResult` is a frozen dataclass:

```python
result.text         # str  — full text in reading order, joined by "\n"
result.blocks       # list[Block] — text + bbox + confidence per region
result.confidence   # float — average confidence, 0..1
result.engine       # "rapidocr"
result.image_size   # (width, height)
result.is_empty     # True when no text was found
result.to_dict()    # JSON-serializable dict
```

### CLI

```bash
clipocr screenshot.png              # plain text
clipocr screenshot.png --bbox       # text with bounding boxes
clipocr screenshot.png --json       # full result as JSON
clipocr --clip                      # read image from clipboard
clipocr --version
```

Set `CLIPOCR_VERBOSE=1` to keep RapidOCR's INFO logs visible.

## Platform notes

| Platform | Clipboard backend |
| --- | --- |
| macOS | Pillow `ImageGrab.grabclipboard()` |
| Windows | Pillow `ImageGrab.grabclipboard()` |
| Linux (Wayland) | `wl-paste --type image/png` |
| Linux (X11) | `xclip -selection clipboard -t image/png -o` |

On Linux, install `wl-clipboard` (Wayland) or `xclip` (X11) to enable `--clip`.

## Languages

`clipocr` ships with the default RapidOCR PP-OCRv4 mobile models. They handle:

- Simplified & Traditional Chinese
- English (Latin alphabet)
- Numbers and common punctuation

Mixed Chinese + English content works out of the box.

## Development

```bash
git clone https://github.com/lidongyangLeo/clipocr.git
cd clipocr

python3 -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"

pytest                       # unit tests with coverage
pytest -m integration        # also exercise the real RapidOCR engine
ruff check src tests         # lint
```

The test suite enforces **≥ 90% coverage** via `pytest-cov`.

## How it works

```
┌──────────────────┐     ┌─────────────┐     ┌──────────┐
│ image / clipboard │ --> │ RapidOCR    │ --> │ OcrResult │
└──────────────────┘     │ (ONNX, on-device) │  └──────────┘
                         └─────────────┘
```

`clipocr` is a thin shim around RapidOCR. It contributes:

1. A clean Python API (`ocr` / `ocr_clipboard` / `OcrResult`).
2. A cross-platform clipboard adapter.
3. Reading-order block sorting (top-to-bottom rows, left-to-right within a row).
4. A version-tolerant engine output normalizer (works with both legacy `rapidocr-onnxruntime` and the new `rapidocr` >= 2.x).
5. A `clipocr` CLI with stdout-clean defaults.

## Contributing

Issues and PRs welcome. Please run the tests and `ruff check` before submitting.

## License

MIT — see [LICENSE](LICENSE).
