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.

Wrapper is MIT. Core (vendored as a git submodule at vendor/contrek) is AGPLv3. You can find more info in the main repo.

Install

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

git clone --recurse-submodules https://github.com/runout77/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

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}

Coordinates always come back as NumPy arrays, not lists of objects.

Low-level API

Direct bitmap + finder access, for tile-based / synthetic / streaming workflows.

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 "clockwise" - "anticlockwise".

find_polygons_raw() returns a RawProcessResult handle instead of a dict, for feeding into a merger without an intermediate conversion:

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()

SvgStreamingMerger / GeoJsonStreamingMerger write tiles to disk incrementally instead of holding everything in memory:

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()

make_result_from_polygons(polygons, width, height) builds a RawProcessResult from ready-made polygon data (no bitmap involved) — for feeding merger tests or externally-computed geometry.

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.0

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.0
File Size Uploaded
contrek-0.1.0.tar.gz 12.3 MB Details

Release files / contrek-0.1.0.tar.gz

Download URL contrek-0.1.0.tar.gz
Size 12.3 MB
Tags Source
SHA-256 checksum
How to use checksums
fc4c3359df7bbdc09810e98b1bd06a05588d883204b6b7f64a9947e48246bf0b
BLAKE2b-256 checksum
How to use checksums
38cd1938e203c1ff7dff675cca65c5c1dbdfb0ce52445e39ef03a72821af9b86
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

0.1.1

1 release file

This release

0.1.0 This release

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