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, siting="cosited", 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 halves chroma along each row and 420 along each row and column, writing each sample to every pixel it covers, the shape pack reads. siting="cosited", the default, places chroma on the even samples (and lines) as SMPTE ST 2036-1 and BT.709 do, filtered [1 2 1] / 4; siting="centred" averages each pair instead. 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, subsampling=None, siting="cosited", xp=numpy) — the inverse, to within half a code per component; returns float32 RGB clamped to [0, 1]. For a subsampled array, co-sited chroma is interpolated between samples and centred chroma repeated over its pixels. Without a layout, subsampling defaults to 444, which reads each pixel's own chroma.
  • 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, subsampling or siting, a bits outside 8–16, fullprotect outside 8, 10 and 12 bits, 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.5.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.5.0
File Size Uploaded Provenance Actions
pypixelpack-0.5.0.tar.gz 66.0 kB With provenance Details for pypixelpack-0.5.0.tar.gz

Built distribution (wheel)

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

Total release size: 84.5 kB

Release files / pypixelpack-0.5.0.tar.gz

Download URL pypixelpack-0.5.0.tar.gz
Size 66.0 kB
Tags Source
SHA-256 checksum
How to use checksums
966d595330ff0428cf72f15f1bd197ad038722df6a2dc2e1c8953f547c3a6302
BLAKE2b-256 checksum
How to use checksums
9c6ad6eb189926684bc903c5a9cccfb00420661c042454f5296258af5362f8a0
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.5.0-py3-none-any.whl

Download URL pypixelpack-0.5.0-py3-none-any.whl
Size 18.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d05620e672fa6157abe1f02efce952ac2cb58eeaff758a06e9cc66f3ca007133
BLAKE2b-256 checksum
How to use checksums
73e998abf29d61de7a4c3736fc9ad368567a68c7a9d76ed36503af64dc3257bd
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.5.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.5.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