Skip to main content

contrek-python

Python bindings for Contrek, a fast raster-to-vector polygon tracing engine written in C++. This wraps the C++ core with pybind11 so you can call it from Python without losing the performance. Coordinates come back as NumPy arrays and the underlying engine is multi-threaded. You can find more info in the main repo.

Wrapper is MIT. Core (vendored as a git submodule at vendor/contrek) is AGPLv3.

Install

Source only, no prebuilt wheels — pip install compiles the core locally and tunes it for your CPU (-march=native, on by default in the core's own CMakeLists). Needs a C++17 compiler and CMake.

pip install contrek

For development (editable install, running tests):

git clone --recurse-submodules https://github.com/<your-user>/contrek-python.git
cd contrek-python
python3 -m venv .venv && source .venv/bin/activate
pip install -e ".[test]"
pytest

Linux/macOS only (POSIX threads).

High-level API

Trace polygons from an image file in one call.

import contrek

result = contrek.contour("image.png", threads=4, tiles=4, treemap=True)
print(result.groups, result.width, result.height)

for poly in result.polygons:
    print(poly.outer)   # numpy int32 (N, 2)
    print(poly.inner)   # list[numpy int32 (N, 2)]
    print(poly.bounds)  # {min_x, min_y, max_x, max_y, is_empty}

Low-level API

Trace polygons from a bitmap you build yourself — either a PNG file (FastPngBitmap) or an in-memory pattern string (Bitmap, useful for synthetic tiles or tests).

bitmap = contrek.Bitmap(pattern_string, width)  # or contrek.FastPngBitmap(path)

result = contrek.find_polygons(
    bitmap,
    options={"versus": "clockwise", "bounds": True, "compress": {"linear": True}},
    target_color=ord("0"),
    mode=contrek.MatchMode.EXACT_COLOR,
)

versus accepts "a"/"o" for "anticlockwise"/"clockwise".

Merging tiles in memory

find_polygons_raw() is the same as find_polygons() but returns a RawProcessResult handle instead of a dict, so it can be fed straight into a merger without converting to Python types first. VerticalMerger/HorizontalMerger combine multiple tiles into one result, in memory.

tile = contrek.find_polygons_raw(bitmap, options={...}, target_color=..., mode=...)
tile2 = contrek.find_polygons_raw(bitmap, options={...}, target_color=..., mode=...)

merger = contrek.VerticalMerger(options={"bounds": True})   # or HorizontalMerger
merger.add_tile(tile)
merger.add_tile(tile2)
result = merger.process_info()

Streaming to disk

SvgStreamingMerger/GeoJsonStreamingMerger write tiles to a file incrementally as they arrive, instead of holding every polygon in memory — useful for very large or many-tile jobs.

merger = contrek.SvgStreamingMerger(options={"bounds": True}, output_path="out.svg", width=18, height=11)
for i, tile in enumerate(tiles):
    merger.add_tile(tile, flush=(i == len(tiles) - 1))
result = merger.process_info()

Building results from raw polygon data

make_result_from_polygons(polygons, width, height) builds a RawProcessResult straight from polygon coordinates you already have — no bitmap or tracing involved. Useful for testing mergers, or feeding in geometry computed elsewhere.

Tests

tests/ also doubles as usage examples — see the various test_*.py files for more ways to call the API.

License

  • Wrapper: MIT (LICENSE)
  • Core (submodule): AGPLv3 — details

Release files for contrek 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for contrek 0.1.1
File Size Uploaded
contrek-0.1.1.tar.gz 12.4 MB Details

Release files / contrek-0.1.1.tar.gz

Download URL contrek-0.1.1.tar.gz
Size 12.4 MB
Tags Source
SHA-256 checksum
How to use checksums
6328f16840e5babd9210dd70accda4fed11142d02da799216dfae4129cd4e30f
BLAKE2b-256 checksum
How to use checksums
567101fafca018721c50b8980405f3a64e78932b3c14bdd1c903d2ffe9ac5c53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Release history Release notifications | RSS feed

0.1.12

1 release file

0.1.11

1 release file

0.1.10

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

This release

0.1.1 This release

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page