Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ports/python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__pycache__/
*.pyc
build/
*.egg-info/
hello.png
67 changes: 64 additions & 3 deletions ports/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Comb Code by Avi Shor. Free to use, with or without credit. See LICENSE.md in th
filled-corner gap pattern, fixed ring and pilot cells — exactly as the live site.
- **Decoder**: reads a clean/perfect PNG produced by any conforming encoder
(this port, the Java port, or the live site's creator) and returns the payload
bytes with header CRC-4 and payload CRC-32 verification. This is not the full
robust camera pipeline (no perspective/glare/blur recovery).
bytes with header CRC-4 and payload CRC-32 verification. `decodePNG` is for clean
images; photos go through the reader (see Photo reader).
- **CLI** for both.

## Usage
Expand Down Expand Up @@ -57,7 +57,7 @@ As a library: `import prism` → `prism.encode(payload, size=-1, ecc=1, mode=0)`
## Known limits (honest)

- The decoder expects a clean, axis-aligned PNG straight from an encoder. Photos
of codes need the site's full reader.
of codes need the reader in `reader/`.
- The live site's own readers currently fail on the largest multi-block clean
codes (e.g. ~1 KB+ at size 4-5) **even on PNGs the site's own creator made**;
this decoder reads those fine. That is a live-reader limitation, not a port
Expand Down Expand Up @@ -98,3 +98,64 @@ Key files accept this port's JSON and the live site's `{"publicKey":{...},"priva
Python; Python-sealed (128 MiB/t=3) opens in JS.
- AES-GCM matches NIST vectors; Argon2id matches hash-wasm byte for byte;
`strength()` matches the JS heuristic exactly on the tested set.

## Install

The encoder, decoder and Comb Code Secure need only the standard library
(Python 3.8+). Copy the modules, or from this directory:

```bash
pip install .
```

The photo reader also needs numpy and Pillow: `pip install ".[reader]"`.

## Example

`examples/encode_decode.py`:

```python
import prism

png, _ = prism.encodePNG("Hello from Comb Code".encode("utf-8"), r=12)
payload, info = prism.decodePNG(png)
print(payload.decode("utf-8"))
```

## API reference (`prism`)

- `prism.encode(payload, size=-1, ecc=1, mode=0, type_=0, logo=0)` returns a code
object. `size` -1 is automatic, 0..5 fixed; `ecc` 0..3 is 10/20/30/40% repair
data; `mode` 0 color, 1 black-and-white; `type_` 0 text, 1 binary.
- `prism.encodePNG(payload, r=12, **kw)` returns `(png_bytes, code)`; `r` is pixels
per cell unit.
- `prism.decodePNG(png_bytes)` returns `(payload, info)`. The payload is only
returned when the header CRC-4 and payload CRC-32 pass.
- `prism.capacity(size, ecc, mode)` returns the payload capacity.

## Photo reader

`reader/` reads codes from photos: detection, hypothesis search, Reed-Solomon
recovery, palette-mix and distance-fit recovery, and the page retry chain. It is a
port of the live reader (`fast-reader.html`).

```python
import sys; sys.path.insert(0, "reader")
from PIL import Image
import read

result = read.read(Image.open("photo.jpg").convert("RGBA"))
print(bytes(result["bytes"]).decode("utf-8"))
```

`examples/read_photo.py photo.jpg` does the same from the command line. It raises
when no code is read.

Limits: canvas resizes in the retry chain are approximated with Pillow, time budgets
are not enforced, and the reader is slow on hard photos (blurred scenes can take
minutes). Synthetic bench scenes only, no physical-camera tests.

## License

Comb Code, Copyright (c) 2026 Avi Shor. Free to use, with or without credit.
No warranty.
21 changes: 21 additions & 0 deletions ports/python/examples/encode_decode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Comb Code Python example: encode a text payload to a PNG, decode it back.
import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
import prism

text = 'Hello from Comb Code'

# Encode: automatic size, 20% repair data, color mode, 12 px per cell unit.
png, _ = prism.encodePNG(text.encode('utf-8'), r=12)
with open('hello.png', 'wb') as f:
f.write(png)
print('encoded', len(text), 'bytes -> hello.png')

# Decode it back. The payload is only returned when its CRC-32 passes.
payload, info = prism.decodePNG(png)
decoded = payload.decode('utf-8')
print('decoded', len(decoded), 'bytes:', decoded)
print('round-trip', 'OK' if decoded == text else 'MISMATCH')
sys.exit(0 if decoded == text else 1)
13 changes: 13 additions & 0 deletions ports/python/examples/read_photo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Comb Code Python example: read a code from a photo.
# usage: python3 examples/read_photo.py photo.jpg
# Needs numpy and Pillow (pip install numpy pillow).
import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'reader'))
from PIL import Image
import read

img = Image.open(sys.argv[1]).convert('RGBA')
result = read.read(img)
print(bytes(result['bytes']).decode('utf-8', 'replace'))
19 changes: 19 additions & 0 deletions ports/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"

[project]
name = "comb-code"
version = "1.0.0"
description = "Comb Code encoder, decoder, Comb Code Secure and photo reader."
readme = "README.md"
requires-python = ">=3.8"
authors = [{ name = "Avi Shor" }]
license = { text = "Free to use, with or without credit" }
urls = { Homepage = "https://getcombcode.github.io/" }

[project.optional-dependencies]
reader = ["numpy", "pillow"]

[tool.setuptools]
py-modules = ["prism", "prism_secure", "prism_secure_cli"]
Loading