Skip to main content

A Polars plugin for vision/array operations

Project description

polars-cv

ℹ️ Note: This is a largely AI developed project and still in its early stages. Use at your own discretion.

A Polars plugin for vision/array operations, powered by view-buffer.

Features

  • Lazy Pipeline Definition: Define image processing pipelines outside DataFrame context
  • Expression Arguments: Use Polars expressions for dynamic, per-row parameters
  • Zero-Copy Where Possible: Leverages view-buffer's stride-aware operations
  • Multiple Source/Sink Formats: PNG, JPEG, NumPy, PyTorch, and more

Installation

# From source (requires Rust toolchain)
cd polars-cv
maturin develop --release

# Or with pip (once published)
pip install polars-cv

ℹ️ Note:
Polars typically operates on relatively small row sizes, which may require changing the POLARS_IDEAL_MORSEL_SIZE environment variable to a smaller value, to avoid memory allocation issues.
You can set it like this before importing polars:

import os
os.environ["POLARS_IDEAL_MORSEL_SIZE"] = "10"

Quick Start

import polars as pl
from polars_cv import Pipeline

# Define a static pipeline
pipe = (
    Pipeline()
    .source("image_bytes")
    .resize(height=224, width=224)
    .grayscale()
    .normalize(method="minmax")
    .sink("numpy")
)

# Apply to DataFrame
df = pl.DataFrame({"images": [img1_bytes, img2_bytes]})
result = df.with_columns(processed=pl.col("images").cv.pipeline(pipe))

Dynamic Pipelines

Use Polars expressions for per-row parameter values:

# Dynamic pipeline with expression arguments
pipe = (
    Pipeline()
    .source("image_bytes")
    .resize(height=pl.col("target_h"), width=pl.col("target_w"))
    .crop(top=pl.col("crop_y"), left=pl.col("crop_x"), height=100, width=100)
    .sink("numpy")
)

df = pl.DataFrame({
    "images": [img1_bytes, img2_bytes],
    "target_h": [224, 256],
    "target_w": [224, 256],
    "crop_x": [10, 20],
    "crop_y": [5, 15],
})

result = df.with_columns(processed=pl.col("images").cv.pipeline(pipe))

Pipeline Operations

Source Formats

Format Description
image_bytes Decode PNG/JPEG/WebP (auto-detect)
blob VIEW protocol binary
raw Raw bytes (requires dtype)
file_path Read from file path

Operations

View Operations (Zero-Copy)

  • transpose(axes) - Permute dimensions
  • reshape(shape) - Reshape array
  • flip(axes) / flip_h() / flip_v() - Flip along axes
  • crop(top, left, height, width) - Crop region

Compute Operations

  • cast(dtype) - Change data type
  • scale(factor) - Multiply by factor
  • normalize(method) - MinMax or ZScore normalization
  • clamp(min, max) - Clamp to range

Image Operations

  • resize(height, width, filter) - Resize image
  • grayscale() - Convert to grayscale
  • threshold(value) - Binary threshold
  • blur(sigma) - Gaussian blur

Sink Formats

Format Description
numpy NumPy-compatible bytes
torch PyTorch-compatible bytes
png Re-encode as PNG
jpeg Re-encode as JPEG (with quality)
blob VIEW protocol (for chaining)
array Polars Array type (fixed shape)
list Polars nested List (variable shape)

Shape Hints

Provide shape information to help pipeline planning:

pipe = (
    Pipeline()
    .source("image_bytes")
    .assert_shape(height=256, width=256, channels=3)
    .resize(height=224, width=224)
    .sink("numpy")
)

Working with List Columns

For batch processing (list of images per row):

batch_df = pl.DataFrame({
    "image_batches": [[img1, img2], [img3, img4, img5]],
})

result = batch_df.with_columns(
    pl.col("image_batches").list.eval(
        pl.element().cv.pipeline(pipe)
    )
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

polars_cv-0.4.0-cp39-abi3-manylinux_2_34_aarch64.whl (9.8 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.34+ ARM64

polars_cv-0.4.0-cp39-abi3-macosx_11_0_arm64.whl (7.2 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

Details for the file polars_cv-0.4.0-cp39-abi3-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for polars_cv-0.4.0-cp39-abi3-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 6c85eef31b6f4d5702719b82ba92dcaea010e7de6689e50a92396c7ee4c88446
MD5 d590e5eb1585331a4a84982c30be8b58
BLAKE2b-256 e3e0aa90cd1d79284e1e0272fd1b60d0280d570a58593896f5f32be651047487

See more details on using hashes here.

File details

Details for the file polars_cv-0.4.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_cv-0.4.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 69b38e53a6d9786e49a3bc1281f74e115957b90f803462d0d134ef3bf3ff86f5
MD5 c127178928075ac2edbfc3759edac458
BLAKE2b-256 ea0070c78b5f908948d8b27ab96732e33bcd0a4c2a6702356e33ef56b01bf034

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page