Image transformation and codec
This topic describes the operators for image codec, format conversion, size transformation, and pixel processing in the Python DataFrame API.
Before you begin
-
Available only in Realtime Compute Engine VVR 11.8 and later.
-
Encoded image columns use
DataType.binary(), and decoded image columns useDataType.image(). -
image_compressandimage_convert_formatcan process encoded images directly. Other operators usually require you to first callimage_decodeto obtain aDataType.image()column, and then callimage_encodeas needed to convert the result back to an encoded image. -
"Input type" and "Return type" use DataFrame API types. Parameter tables use Python types.
Operator overview
|
Category |
Operator |
Description |
|
Codec |
Decode raw image bytes into |
|
|
Encode a decoded image into compressed bytes or a Data URL. |
||
|
Encoding conversion |
Recompress encoded image bytes. |
|
|
Convert the encoding format. |
||
|
Type conversion |
Convert the image mode. |
|
|
Convert an image to a fixed-shape float32 tensor. |
||
|
Sizing and cropping |
Resize an image to specified dimensions. |
|
|
Rescale an image proportionally. |
||
|
Crop an image region. |
||
|
Detect and crop black borders. |
||
|
Image enhancement |
Flip an image. |
|
|
Blur processing. |
||
|
Adjust brightness, contrast, and saturation. |
||
|
Image background processing |
Remove the image background. |
Common runtime parameters
Function signatures retain the runtime parameters that each operator actually supports. To avoid repetition, the operator parameter tables describe only business parameters. Runtime parameters are described below.
|
Parameter |
Type |
Default |
Applies to |
Description |
|
|
|
|
All operators on this page |
UDF concurrency. |
|
|
|
|
|
Model sharing mode. |
|
|
|
|
|
Batch size of the Batch UDF. |
|
|
|
|
|
Number or fraction of GPUs requested per model instance, for example, |
|
|
|
|
|
GPU type. Configure this parameter together with |
Codec and type conversion
image_decode
Decode raw image bytes into the Flink built-in image type DataType.image().
Input type: DataType.binary(), a column of encoded image bytes.
Function signature:
image_decode(
*columns,
on_error="raise",
mode=None,
pixel_limit=None,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
|
|
|
|
|
Target image mode. |
|
|
|
|
Maximum number of pixels. Inputs that exceed this limit are handled according to |
Return type: DataType.image().
from pyflink.dataframe import col
from pyflink.multimodal.operators import image_decode
result = df.with_column(
"image",
image_decode(
col("image_bytes"),
on_error="null",
mode="RGB",
pixel_limit=8192 * 8192
)
)
image_encode
Encode a decoded image into compressed bytes or a Base64 Data URL string.
Input type: DataType.image(), a column of decoded images.
Function signature:
image_encode(
*columns,
format=None,
quality=85,
output="bytes",
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
Encoding quality. Valid values: |
|
|
|
|
|
|
|
|
|
Target format, for example, |
Return type:
-
output="bytes":DataType.binary(). -
output="data_url":DataType.string().
from pyflink.multimodal.operators import image_encode
result = (
df
.with_column(
"jpeg_bytes",
image_encode(col("image"), format="JPEG", quality=90)
)
.with_column(
"data_url",
image_encode(col("image"), format="PNG", output="data_url")
)
)
image_compress
Recompress an encoded image with the specified format and quality factor.
Input type: DataType.binary(), a column of encoded images.
Function signature:
image_compress(
*columns,
quality=85,
format="JPEG",
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
Compression quality. Valid values: |
|
|
|
|
Output format, for example, |
Return type: DataType.binary().
from pyflink.multimodal.operators import image_compress
result = df.with_column(
"compressed",
image_compress(
col("image_bytes"),
quality=60,
format="JPEG"
)
)
image_convert_format
Convert an encoded image to a different encoding format.
Input type: DataType.binary(), a column of encoded images.
Function signature:
image_convert_format(
*columns,
format,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
Required |
Target format, for example, |
Return type: DataType.binary().
from pyflink.multimodal.operators import image_convert_format
result = df.with_column(
"png_bytes",
image_convert_format(
col("image_bytes"),
format="PNG"
)
)
image_convert_mode
Convert an image to the target color mode.
Input type: DataType.image(), a column of decoded images.
Function signature:
image_convert_mode(
*columns,
mode,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
Required |
Target mode, for example, |
Return type: DataType.image().
from pyflink.multimodal.operators import image_convert_mode
result = df.with_column(
"grayscale",
image_convert_mode(
col("image"),
mode="L"
)
)
image_to_tensor
Convert an image to a fixed-shape float32 tensor. This operator does not automatically resize images or convert their modes. It returns None if the dimensions or channel count do not match.
Input type: DataType.image(), a column of decoded images.
Function signature:
image_to_tensor(
*columns,
width,
height,
mode="RGB",
layout="CHW",
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
Required |
The width that input images must have. |
|
|
|
Required |
The height that input images must have. |
|
|
|
|
Declares the channel count of the output tensor. For example, |
|
|
|
|
|
uint8 or uint16 pixels are converted to float32 and scaled to [0, 1].
Return type: DataType.tensor("float32", shape=...). The output shape is determined by width, height, mode, and layout.
from pyflink.multimodal.operators import (
image_convert_mode,
image_resize,
image_to_tensor,
)
result = df.with_column(
"tensor",
image_to_tensor(
col("image"),
width=224,
height=224,
mode="RGB",
layout="CHW"
)
)
Sizing and cropping
image_resize
Resize an image to specified dimensions. This scaling is non-proportional. For proportional scaling, use image_rescale.
Input type: DataType.image(), a column of decoded images.
Function signature:
image_resize(
*columns,
width,
height,
method="lanczos",
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
Required |
Target width. Must be a positive integer. |
|
|
|
Required |
Target height. Must be a positive integer. |
|
|
|
|
Resampling method. Supported values: |
Return type: DataType.image().
from pyflink.multimodal.operators import image_resize
result = df.with_column(
"resize_image",
image_resize(
col("image"),
width=256,
height=256
)
)
image_rescale
Rescale an image proportionally.
Input type: DataType.image(), a column of decoded images.
Function signature:
image_rescale(
*columns,
scale,
method="lanczos",
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
Required |
Scale factor. Must be greater than 0. For example, |
|
|
|
|
Resampling method. Supported values: |
Return type: DataType.image().
from pyflink.multimodal.operators import image_rescale
result = df.with_column(
"half_size",
image_rescale(
col("image"),
scale=0.5
)
)
image_crop
Crop an image region.
Input type: DataType.image(), a column of decoded images.
Function signature:
image_crop(
*columns,
crop_coords=None,
crop_type="center",
crop_ratio=(0.8, 0.8),
crop_size=None,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
Absolute pixel box |
|
|
|
|
Cropping mode. Supported values: |
|
|
|
|
Width and height ratios |
|
|
|
|
Fixed output |
Return type: DataType.image().
from pyflink.multimodal.operators import image_crop
result = df.with_column(
"roi",
image_crop(
col("image"),
crop_coords=(100, 100, 500, 500)
)
)
image_crop_black_border
Detect and crop black borders from an image.
Input type: DataType.image(), a column of decoded images.
Function signature:
image_crop_black_border(
*columns,
threshold=None,
detect_algorithm="auto",
black_threshold=None,
edge_sensitivity=1.0,
min_border_size=1,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
A compatibility parameter for |
|
|
|
|
Detection algorithm. Supported values: |
|
|
|
|
Luminance threshold from 0 to 255. |
|
|
|
|
Edge detection sensitivity. Valid values: |
|
|
|
|
Minimum black border width, in pixels, that triggers cropping. |
Return type: DataType.image().
from pyflink.multimodal.operators import image_crop_black_border
result = df.with_column(
"clean",
image_crop_black_border(
col("image"),
black_threshold=15
)
)
Image enhancement
image_flip
Flip an image.
Input type: DataType.image(), a column of decoded images.
Function signature:
image_flip(
*columns,
mode="horizontal",
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
Flip direction. Supported values: |
Return type: DataType.image().
from pyflink.multimodal.operators import image_flip
result = df.with_column(
"flipped",
image_flip(
col("image"),
mode="vertical"
)
)
image_blur
Apply blur processing to an image.
Input type: DataType.image(), a column of decoded images.
Function signature:
image_blur(
*columns,
radius=2,
blur_type="gaussian",
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
Blur radius. Must be a finite non-negative number. This parameter does not take effect when |
|
|
|
|
Blur method. Supported values: |
Return type: DataType.image().
from pyflink.multimodal.operators import image_blur
result = df.with_column(
"blurred",
image_blur(
col("image"),
radius=5,
blur_type="gaussian"
)
)
image_adjust_color
Adjust brightness, contrast, and saturation.
Input type: DataType.image(), a column of decoded images.
Function signature:
image_adjust_color(
*columns,
brightness=1.0,
contrast=1.0,
saturation=1.0,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
Brightness factor. Must be non-negative. |
|
|
|
|
Contrast factor. Must be non-negative. |
|
|
|
|
Saturation factor. Must be non-negative. |
Actual color adjustment is not supported for the L16 mode.
Return type: DataType.image().
from pyflink.multimodal.operators import image_adjust_color
result = df.with_column(
"enhanced",
image_adjust_color(
col("image"),
brightness=1.1,
contrast=1.2,
saturation=0.9
)
)
image_remove_background
Remove the background of an image and return an image with a transparent background or filled with a specified background color.
Input type: DataType.image(), a column of decoded images.
Dependency: pip install rembg
Function signature:
image_remove_background(
*columns,
alpha_matting=False,
alpha_matting_foreground_threshold=240,
alpha_matting_background_threshold=10,
alpha_matting_erode_size=10,
bgcolor=None,
model_sharing=None,
concurrency=None,
batch_size=None,
num_gpus=None,
gpu_type=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
Whether to enable alpha matting. |
|
|
|
|
Foreground threshold for alpha matting. Valid values: |
|
|
|
|
Background threshold for alpha matting. Valid values: |
|
|
|
|
Erosion kernel size for alpha matting. Must be a positive integer. |
|
|
|
|
RGBA background color |
Return type: DataType.image().
from pyflink.multimodal.operators import image_remove_background
result = df.with_column(
"foreground",
image_remove_background(
col("image"),
alpha_matting=True,
bgcolor=(255, 255, 255, 255)
)
)