Skip to main content

pypixelpack

Pixel layouts and wire encodings for video I/O, device-free.

The bytes a video frame becomes on a wire — v210, r210, the ST 2110-20 pgroup — and the encoding between a frame's RGB and those bytes: colour matrix, range, chroma subsampling. Written once against a caller-supplied array namespace, so the same source packs on numpy for a host frame and on torch for a frame that never leaves a GPU.

Extracted from pydecklink's packing module and a GPU render pipeline's colorspace node, which held the same layout twice. This repository's SPEC.md and ROADMAP.md govern the package.

Installation

uv add pypixelpack

numpy is the only dependency. torch is a namespace the caller supplies, never a dependency of this package (§spec:backend).

Usage

import numpy as np
from pypixelpack import decode, encode, pack, unpack

rgb = np.zeros((1080, 1920, 3), dtype=np.float32)  # [R, G, B] in [0, 1]
codes = encode(rgb, subsampling="422")  # (H, W, 3) uint16 [Y, Cb, Cr]
data = pack(codes, "v210", row_bytes=5120)  # 1-D uint8, DMA-ready
back = unpack(data, "v210", width=1920, height=1080, row_bytes=5120)
rgb_again = decode(back, subsampling="422")  # float32 [R, G, B] in [0, 1]

On a GPU host, pass the namespace and the arrays stay resident:

import torch

codes = encode(rgb_on_cuda, subsampling="422", xp=torch)
data = pack(codes, "v210", row_bytes=5120, xp=torch)

API

  • pack(pixels, layout, row_bytes, *, xp=numpy) — (H, W, channels) integer samples to a 1-D uint8 buffer of H × row_bytes in layout, on the input's device (§spec:layouts). RGB layouts take [R, G, B]; v210 and 2vuy take [Y, Cb, Cr] with chroma read from even columns; ay10 takes [Y, Cb, Cr, A] with a per-pixel alpha.
  • unpack(data, layout, width, height, row_bytes, *, xp=numpy) — the inverse; unpack(pack(x)) == x for every layout. Returns uint8 for 8-bit layouts and uint16 otherwise.
  • row_bytes(layout, width) — the smallest row_bytes that holds a line.
  • channels(layout) — 3, or 4 for a layout in ALPHA_LAYOUTS.
  • LAYOUTS — the layout table, name → (pixels per group, bytes per group, bit depth): argb, bgra, r210, r10b, r10l, v210, 2vuy, r12b, r12l, ay10.

pack raises ValueError for an unknown layout, a row_bytes shorter than the packed line, or a sample above the layout's bit depth; the last check is skipped under torch.compile, where a compiled caller trusts its own inputs (§spec:backend).

Encoding

  • encode(rgb, *, matrix="bt709", levels="narrow", layout=None, bits=None, subsampling=None, xp=numpy) — (H, W, 3) float RGB in [0, 1] to (H, W, 3) uint16 [Y, Cb, Cr] (§spec:encoding). matrix is bt601, bt709 or bt2020; levels is narrow (luma 16–235, chroma 16–240 at 8 bits, shifted up by bits - 8), full, or fullprotect (full range clamped off the codes an SDI-derived interface reserves for timing: 4–1019 at 10 bits; defined at 8, 10 and 12 bits and refused at any other depth). layout="v210" selects the depth and subsampling that wire format expects — 10-bit 4:2:2; 2vuy is 8-bit 4:2:2 — and a bits or subsampling that contradicts it is refused; without a layout they default to 10 and 444. 422 averages chroma over each horizontal pair and writes it to both pixels, the shape pack reads. Rounding is half to even, arithmetic is float32 on every backend, and codes clamp to the level range's span.
  • decode(ycbcr, *, matrix, levels, layout=None, bits=None, xp=numpy) — the inverse, per pixel, to within half a code per component; returns float32 RGB clamped to [0, 1].
  • encode_rgb(rgb, *, levels="narrow", bits=10, xp=numpy) — (H, W, 3) float RGB in [0, 1] to (H, W, 3) uint16 [R, G, B] codes, every component on the luma span: 64–940 for 10-bit narrow range, 0–1023 for full, 4–1019 for fullprotect. Rounding and clamping follow encode.
  • decode_rgb(codes, *, levels="narrow", bits=10, xp=numpy) — the inverse, to within half a code per component; returns float32 RGB clamped to [0, 1]. encode_rgb(decode_rgb(c)) == c for every legal code.
  • encoding_for(layout) — the (bits, subsampling) a layout carries; ENCODINGS is the table behind it.
  • legal_codes(*, levels="narrow", bits=10) — the (luma, chroma) code spans as range objects; len(legal_codes().luma) is 877, the levels 10-bit narrow range can represent.
  • MATRICES — name → (KR, KB): bt601, bt709, bt2020.

encode and decode raise ValueError for an unknown matrix, levels or subsampling, a bits outside 8–16, or a layout with no component encoding. encode_rgb and decode_rgb raise it for an unknown levels or a bits outside 8–16.

Development

uv sync
uv run ruff format --check . && uv run ruff check . && uv run pyright && uv run pytest

uv sync installs torch (CPU) into the dev group so the parity suite runs; without it those tests skip. To prove the wheel on numpy alone:

uv build
uv run --isolated --no-project --with dist/*.whl python tools/check_core_install.py

License

BSD-3-Clause. See LICENSE.

Metadata

Release files for pypixelpack 0.4.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 pypixelpack 0.4.0
File Size Uploaded Provenance Actions
pypixelpack-0.4.0.tar.gz 61.9 kB With provenance Details for pypixelpack-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pypixelpack 0.4.0
File Interpreter ABI Platform Actions
pypixelpack-0.4.0-py3-none-any.whl Python 3 none any Details for pypixelpack-0.4.0-py3-none-any.whl

Total release size: 78.8 kB

Release files / pypixelpack-0.4.0.tar.gz

Download URL pypixelpack-0.4.0.tar.gz
Size 61.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ce47fd96b03231c555221baeaae8208fe2493c34474b5af759c371995fb79292
BLAKE2b-256 checksum
How to use checksums
379df53915b98581f62d5836f5b4b47ce73412153839376b395356b9631b289a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

Release files / pypixelpack-0.4.0-py3-none-any.whl

Download URL pypixelpack-0.4.0-py3-none-any.whl
Size 16.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d6d62425620d6b5283c7fefe29623d43f941079dc2b3d10f4361ea76743b042
BLAKE2b-256 checksum
How to use checksums
c799c2e8061fbd311ec7b9248d09abba5c945d7765640bd6899d436460179971
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

Security for pypixelpack 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pypixelpack-0.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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