Metadata-Version: 2.4
Name: signature-improver
Version: 1.0.0
Summary: Robust, CLI-friendly signature enhancement: isolate, clean, smooth and recolour signatures.
Author: DigantaDey
License-Expression: Apache-2.0
Project-URL: Homepage, https://digantadey.in
Project-URL: Repository, https://github.com/DigantaDey/signature-improver
Project-URL: Issues, https://github.com/DigantaDey/signature-improver/issues
Keywords: signature,image,opencv,cli,binarization
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
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: Topic :: Multimedia :: Graphics
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Utilities
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: opencv-python-headless>=4.5
Requires-Dist: numpy>=1.20
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Dynamic: license-file

# signature-improver

<p align="center">
  <strong>Fast, robust signature enhancement — for the command line and the Python ecosystem.</strong>
</p>

<p align="center">
  Isolate the strokes, remove noise, smooth the edges and recolour a scanned
  signature. Installs with <code>pip</code>, ships a <code>signature-enhancer</code>
  command, and is usable as a library. Built for personal projects and serious
  development work alike.
</p>

<p align="center">
  <a href="https://github.com/DigantaDey/signature-improver">⭐ View on GitHub</a>
  &nbsp;·&nbsp;
  <a href="https://digantadey.in">By Diganta Dey</a>
</p>

---

**Repository:** <https://github.com/DigantaDey/signature-improver>
**Author:** [Diganta Dey](https://digantadey.in)
**License:** Apache License 2.0

## Why this tool

Signature cleanup is fiddly: uneven lighting, stray speckles, jagged edges and
inconsistent ink all break naive approaches. `signature-improver` uses a
vectorised OpenCV/NumPy pipeline that handles these cases reliably, then exposes
the result through both a zero-config CLI and a clean Python API.

## Features

- **Robust binarisation** — Otsu, adaptive (local) and manual thresholding, with
  automatic polarity detection for dark-on-light *and* light-on-dark strokes.
- **Noise removal** — connected-component despeckling plus optional morphological
  opening/closing to drop speckles and fill gaps.
- **Smooth edges** — a Gaussian-blurred alpha channel produces anti-aliased
  strokes instead of jagged pixels.
- **Luminance-preserving colour** — strokes are tinted while keeping the original
  shading and pen pressure; a `clean` mode paints solid ink for documents.
- **Flexible output** — composite over the original background, white, or a
  transparent RGBA PNG; optionally crop to the signature bounding box.
- **CLI + library** — use `signature-enhancer` on the command line or call
  `enhance_signature` directly from Python. Batch and glob support built in.

## Install

Install with `pip` straight from the GitHub repository — no PyPI account or
publishing required.

### Option 1 — from GitHub (recommended)

```bash
pip install "git+https://github.com/DigantaDey/signature-improver.git"
```

### Option 2 — from a local clone

```bash
git clone https://github.com/DigantaDey/signature-improver.git
cd signature-improver
pip install .
```

### Option 3 — isolated CLI with pipx (personal use)

```bash
pipx install "git+https://github.com/DigantaDey/signature-improver.git"
```

### Option 4 — editable install for development

```bash
git clone https://github.com/DigantaDey/signature-improver.git
cd signature-improver
pip install -e ".[dev]"
```

After installing, the `signature-enhancer` command is on your PATH. You can also
run it without installing globally:

```bash
python -m signature_enhancer --help
```

## Quick start

```bash
# Blue, smoothed signature on a white background
signature-enhancer photo.jpg -o blue.png

# Transparent PNG, cropped to the signature, solid dark ink
signature-enhancer scan.png -o clean.png --background transparent --crop \
    --mode clean --color "#000000"

# Batch process a folder of scans
signature-enhancer scans/*.jpg --output-dir out --mode clean

# Tune binarisation and denoise settings
signature-enhancer sig.png -o out.png --threshold adaptive --min-area 32 --smooth 5
```

## Options

| Option | Choices / default | Description |
| --- | --- | --- |
| `inputs` | files / globs | One or more images to process. |
| `-o, --output` | | Output file (single input only). |
| `--output-dir` | | Write all results into this directory. |
| `--mode` | `colorize` (default), `clean` | `colorize` tints preserving luminance; `clean` paints solid strokes. |
| `--color` | `#RRGGBB` / name, default `#1050ff` | Target stroke colour. |
| `--background` | `keep` (default), `white`, `transparent` | Background handling. `transparent` writes RGBA PNG. |
| `--threshold` | `otsu` (default), `adaptive`, `manual` | Binarisation strategy. |
| `--threshold-value` | `127` | Grey level for `manual`. |
| `--polarity` | `auto` (default), `dark`, `light` | Dark-on-light vs light-on-dark. |
| `--min-area` | `16` | Drop components smaller than this (despeckle). |
| `--smooth` | `3` | Gaussian blur kernel for edge smoothing (odd, ≥1). |
| `--open` / `--close` | `0` | Morphological opening/closing kernel sizes. |
| `--crop` | | Trim output to the signature's bounding box. |
| `-q, --quiet` | | Only report errors. |

## How it works

1. **Binarise** — the grey image becomes a stroke mask using Otsu (robust to
   uneven lighting), adaptive (local), or a manual threshold. Polarity is
   auto-detected so dark-on-light and light-on-dark signatures both work.
2. **Despeckle** — connected components smaller than `--min-area` are removed.
3. **Clean** — optional morphological opening (thin noise) / closing (gaps).
4. **Smooth** — a Gaussian blur on the mask produces a soft alpha channel, so
   edges are anti-aliased instead of jagged.
5. **Recolour** — strokes are tinted with `--color`. In `colorize` mode the
   target colour is scaled by the original luminance, preserving pen pressure
   and anti-aliasing. In `clean` mode strokes are solid.
6. **Composite** over the original, white, or a transparent alpha channel, then
   optionally crop to the bounding box.

## Use as a library

```python
from signature_enhancer import EnhanceOptions, enhance_signature

result = enhance_signature(
    "photo.jpg",
    "blue.png",
    EnhanceOptions(
        mode="colorize",
        color=(255, 0, 0),          # BGR blue
        background="white",
        crop=True,
    ),
)
print(f"Wrote {result.output_path} ({result.stroke_pixels} stroke pixels)")
```

## Development

```bash
pip install -e ".[dev]"
pytest
```

The test suite covers the core algorithm (all threshold modes, every background,
polarity, cleanup, crop, error handling) and the full CLI surface.

## License

Licensed under the [Apache License 2.0](LICENSE).
