Image#
Image Operations#
Transform#
Image transformation operators.
Transform operators accept either raw image bytes (DataType.binary())
or a built-in IMAGE value (from image_decode()). Pixel transforms
return built-in IMAGE values so chained operators can avoid repeated
JPEG/PNG encode-decode cycles. Boundary operators such as
image_encode(), image_compress(), and image_convert_format()
return encoded image bytes.
Typical pipeline:
from pyflink.multimodal.operators.image_transform import (
image_decode, image_encode, image_resize, image_blur,
)
decode = image_decode()
resize = image_resize(width=512, height=512)
blur = image_blur(radius=2)
encode = image_encode(format="JPEG")
df = df.with_columns("img", decode(col("raw_bytes")))
df = df.with_columns("img", resize(col("img")))
df = df.with_columns("img", blur(col("img")))
df = df.with_columns("jpg", encode(col("img")))
|
Decode encoded image bytes into a decoded image. |
|
Encode an image into compressed bytes or a base64 data URL. |
|
Compress an image to encoded bytes with the specified format and quality. |
|
Convert an image to a different encoded format. |
|
Convert an image to a fixed-shape float32 tensor. |
|
Convert an image to a target mode. |
|
Resize an image to fixed output dimensions. |
|
Rescale an image by a multiplicative factor, preserving aspect ratio. |
|
Crop an image by coordinates, center ratio, or center size. |
|
Detect and remove black borders from an image. |
|
Flip an image horizontally, vertically, or rotate 180 degrees. |
|
Apply blur to an image. |
|
Adjust image brightness, contrast, and saturation. |
|
Remove the background from an image using rembg (U2-Net based). |
Information#
Image information extraction operators.
Operators in this module take raw encoded image bytes or decoded Image
values as input and return numeric values, strings, or booleans - they do
not modify the image.
Without business params (factory function, no args):
from pyflink.multimodal.operators.image_info import image_aspect_ratio
df.with_column("ratio", image_aspect_ratio()(col("img")))
With business params (factory function):
from pyflink.multimodal.operators.image_info import image_hash
phash = image_hash(method="phash")
df.with_column("hash", phash(col("img")))
- Runtime args:
concurrencyis forwarded to the DataFrame UDF runtime.
|
Extract metadata (width, height, channels, mode, encoded format) from an image. |
|
Compute the aspect ratio (width / height) of an image. |
|
Compute a raw sharpness metric using Laplacian variance. |
|
Compute a perceptual hash of an image. |
|
Check if an image can be decoded by the multimodal image codec. |
|
Filter images by pixel dimensions. |
|
Filter images by aspect ratio (width / height). |
|
Filter images by encoded file size in bytes. |
Detection#
Detection and recognition operators.
Operators in this module detect or recognize structured content within images: objects, text, segments, and subplots.
Model-based operators (ImageDetection, ImageSegment, ImageOCR)
are registered as pandas batch UDFs, so Flink batches rows automatically.
image_detect_objects returns axis-aligned boxes x/y/w/h;
image_ocr returns four-point text polygons; image_segment returns
indexed PNG mask bytes; image_detect_subplot returns subplot metadata.
Example:
>>> from pyflink.multimodal.operators.image_detect import (
... image_detect_objects, image_ocr, image_segment, image_detect_subplot,
... )
>>> from pyflink.dataframe import col
>>> detect = image_detect_objects(model="yolov8n", confidence=0.05)
>>> df = df.with_column("objects", detect(col("img")))
>>> ocr = image_ocr(lang=["en", "ch_sim"])
>>> df = df.with_column("text", ocr(col("img")))
- Runtime args:
Model-based pandas factories forward keyword-only
concurrency,batch_size,num_gpus, andgpu_typeto the DataFrame UDF runtime.image_detect_subplotforwardsconcurrency.
|
Create an object detection UDF (YOLO-based). |
|
Create a semantic segmentation UDF (FastSAM-based). |
|
Create an OCR text extraction UDF (EasyOCR-based). |
|
Create a subplot / mosaic detection UDF (OpenCV Hough-based). |
Face#
Face processing operators.
Operators in this module detect, count, or blur faces in image inputs with OpenCV Haar cascade classifiers.
Requires: pip install opencv-python-headless. image_face_blur also
requires pip install Pillow.
- Runtime args:
concurrencyis forwarded to the DataFrame UDF runtime.
Usage:
>>> from pyflink.multimodal.operators.image_face import (
... image_face_detect, image_face_count, image_face_blur,
... )
>>> from pyflink.dataframe import col
>>> detect = image_face_detect(min_neighbors=3)
>>> df = df.with_column("faces", detect(col("img")))
>>> count = image_face_count(min_neighbors=3)
>>> df = df.with_column("face_count", count(col("img")))
>>> blur = image_face_blur(radius=15, min_neighbors=3)
>>> df = df.with_column("blurred", blur(col("img")))
|
Create a face detection UDF (OpenCV Haar Cascade-based). |
|
Create a face count UDF (OpenCV Haar Cascade-based). |
|
Create a face blurring UDF (OpenCV Haar Cascade + PIL-based). |
Quality#
Image quality and safety assessment operators.
Operators in this module score images based on quality, safety, and watermark detection. All operators return float scores.
Example:
>>> from pyflink.multimodal.operators.image_quality import image_quality_score
>>> quality = image_quality_score()
>>> df = df.with_column("quality", quality(col("img")))
>>> from pyflink.multimodal.operators.image_quality import image_nsfw_score
>>> nsfw = image_nsfw_score()
>>> df = df.with_column("nsfw", nsfw(col("img")))
- Runtime args:
Pandas batch UDFs accept
concurrency,batch_size,num_gpus, andgpu_type. Row-level UDFs acceptconcurrency.- Input errors:
Null image inputs produce
Noneoutputs. Corrupt encoded bytes are not suppressed; decode errors propagate to the caller. Usedecode_image(on_error=...)oris_valid_imagebefore these operators when processing untrusted raw bytes.
|
Create an NSFW scoring UDF (HuggingFace image classification-based). |
|
Create an aesthetic quality scoring UDF (HuggingFace aesthetics predictor-based). |
|
Create an image quality scoring UDF (Laplacian + contrast + colorfulness-based). |
|
Create a watermark scoring UDF (HuggingFace image classification-based). |
Embedding#
Image embedding and similarity operators.
Operators in this module generate vector embeddings from images or compute image-text similarity scores using CLIP.
All operators in this module use pandas batch UDFs and run CLIP inference once per input batch.
Example:
>>> from pyflink.multimodal.operators.image_embed import (
... image_embedding, image_text_similarity,
... )
>>> embed = image_embedding(model="ViT-B/32")
>>> df = df.with_column("vector", embed(col("img")))
>>> # Fixed text mode — compare all images against one prompt
>>> sim = image_text_similarity(text="a photo of a cat")
>>> df = df.with_column("score", sim(col("img")))
>>> # Per-row text mode — each row has its own text
>>> sim = image_text_similarity()
>>> df = df.with_column("score", sim(col("img"), col("text")))
- Runtime args:
concurrency,batch_size,num_gpus, andgpu_typeare forwarded to the DataFrame UDF runtime.
Requires: pip install open_clip_torch torch pillow
|
Create an image embedding UDF (CLIP / open_clip-based). |
|
Create an image-text similarity scoring UDF (CLIP / open_clip-based). |
Image Codec#
Codec and format conversion helpers for multimodal data.
Native IMAGE encode/decode conversion helpers for multimodal operators.
|
Return the channel count used by a native IMAGE mode. |
|
Convert a native |
|
Convert an ndarray to a native |
|
Convert a PIL image to a native |
|
Convert an image ndarray to a canonical native IMAGE mode. |
|
Convert ndarray pixels to a PIL Image, converting mode only when requested. |
|
Convert ndarray pixels to a PIL Image, normalizing only when required. |
|
Normalize and validate an encoded image format name. |
|
Validate JPEG/WebP encode quality. |
|
Detect image format from magic bytes. |
|
Convert ndarray pixels to a PIL Image compatible with JPEG output. |
|
Decode an image from raw bytes or a native |
|
Decode an image and return |
|
Decode encoded bytes or native Image input into a native |
|
Encode raw bytes or native |
|
Decode a batch of bytes/native IMAGE inputs into ndarrays, skipping failures. |
|
Decode a batch of bytes/native IMAGE inputs into ndarrays (fail-fast). |