Image properties and filtering
This topic describes the image validity check, metadata extraction, sharpness calculation, perceptual hashing, and filtering operators in the Python DataFrame API.
Before you begin
-
Available only in Realtime Compute Engine VVR 11.8 and later.
-
Validation, metadata extraction, and size filtering of encoded images read only the container header. These lightweight checks do not guarantee that the complete pixel stream can be decoded.
Operator list
|
Category |
Operator |
Description |
|
Validity check |
Determines whether an image passes the validity check. |
|
|
Property extraction |
Extracts the image width, height, number of channels, mode, and more. |
|
|
Calculates the aspect ratio. |
||
|
Calculates image sharpness by using the Laplacian variance. |
||
|
Calculates the perceptual hash of an image. |
||
|
Data filtering |
Filters by pixel dimensions. |
|
|
Filters by aspect ratio. |
||
|
Filters by file size. |
Common runtime parameters
Function signatures include the runtime parameters that each operator supports. To avoid repetition, the operator parameter tables describe only business parameters. The common runtime parameters are described as follows.
|
Parameter |
Type |
Default |
Applies to |
Description |
|
|
|
|
All operators on this page |
The UDF concurrency. |
Validity check and property extraction
is_valid_image
Determines whether an input image passes a lightweight validity check, and optionally checks whether the decoded image can be converted to the specified mode.
Input type: DataType.binary() or DataType.image(). Accepts a column of encoded image bytes or decoded images.
Function signature:
is_valid_image(
*columns,
mode=None,
pixel_limit=None,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
The target mode. When set, the operator checks whether the input can be converted to this mode. |
|
|
|
|
The maximum number of pixels ( |
Null values, corrupted inputs, inputs that exceed pixel_limit, or inputs that cannot be converted to the target mode return False.
Return type: DataType.boolean().
from pyflink.dataframe import col
from pyflink.multimodal.operators import is_valid_image
result = df.filter(
is_valid_image(
col("image_bytes"),
pixel_limit=4096 * 4096
)
)
image_metadata
Extracts the image width, height, number of channels, mode, and encoding format.
Input type: DataType.binary() or DataType.image(). Accepts a column of encoded image bytes or decoded images.
Function signature:
image_metadata(
*columns,
concurrency=None
)
For encoded images, only the container header is read. Null or corrupted inputs return None. Decoded images do not preserve the original container format, so format returns "UNKNOWN".
Return type:
DataType.struct({
"width": DataType.int32(),
"height": DataType.int32(),
"channels": DataType.int32(),
"mode": DataType.string(),
"format": DataType.string()
})
from pyflink.multimodal.operators import image_metadata
result = df.with_column(
"metadata",
image_metadata(col("image_bytes"))
)
image_aspect_ratio
Calculates the aspect ratio width / height.
Input type: DataType.binary() or DataType.image(). Accepts a column of encoded image bytes or decoded images.
Function signature:
image_aspect_ratio(
*columns,
concurrency=None
)
Invalid or corrupted inputs return None.
Return type: DataType.float64().
from pyflink.multimodal.operators import image_aspect_ratio
result = df.with_column(
"aspect_ratio",
image_aspect_ratio(col("image"))
)
image_sharpness
Calculates image sharpness by using the Laplacian variance. A higher value usually indicates sharper edges and richer details, whereas a lower value usually indicates a blurrier image.
Input type: DataType.image(), a column of decoded images.
Function signature:
image_sharpness(
*columns,
max_edge=None,
allow_upscale=False,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
The maximum length of the longest edge, in pixels, allowed before computation. |
|
|
|
|
Whether to allow upscaling small images when |
The value range is [0, +∞). The result measures sharpness only, not a composite image quality score. To compare different images, use the same max_edge and allow_upscale settings.
Return type: DataType.float64().
from pyflink.multimodal.operators import image_sharpness
result = df.with_column(
"sharpness",
image_sharpness(
col("image"),
max_edge=512
)
)
image_hash
Calculates the perceptual hash value of an image.
Input type: DataType.image(), a column of decoded images.
Function signature:
image_hash(
*columns,
method="phash",
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
The hash algorithm. Supported values: |
Return type: DataType.string().
from pyflink.multimodal.operators import image_hash
result = df.with_column(
"perceptual_hash",
image_hash(
col("image"),
method="phash"
)
)
Data filtering
image_size_filter
Filters images by pixel dimensions.
Input type: DataType.binary() or DataType.image(). Accepts a column of encoded image bytes or decoded images.
Function signature:
image_size_filter(
*columns,
min_w=None,
min_h=None,
max_w=None,
max_h=None,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
The minimum width (inclusive). |
|
|
|
|
The minimum height (inclusive). |
|
|
|
|
The maximum width (inclusive). |
|
|
|
|
The maximum height (inclusive). |
At least one boundary must be set. For encoded images, only lightweight metadata is read. Invalid or corrupted inputs return False.
Return type: DataType.boolean().
from pyflink.multimodal.operators import image_size_filter
result = df.filter(
image_size_filter(
col("image_bytes"),
min_w=256,
min_h=256,
max_w=8192,
max_h=8192
)
)
image_shape_filter
Filters images by aspect ratio.
Input type: DataType.binary() or DataType.image(). Accepts a column of encoded image bytes or decoded images.
Function signature:
image_shape_filter(
*columns,
min_ratio=None,
max_ratio=None,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
The minimum aspect ratio |
|
|
|
|
The maximum aspect ratio |
At least one boundary must be set. Invalid or corrupted inputs return False.
Return type: DataType.boolean().
from pyflink.multimodal.operators import image_shape_filter
result = df.filter(
image_shape_filter(
col("image"),
min_ratio=0.5,
max_ratio=2.0
)
)
image_file_size_filter
Filters images by encoded file size. This operator accepts only encoded bytes, not DataType.image().
Input type: DataType.binary(), a column of encoded image bytes.
Function signature:
image_file_size_filter(
*columns,
min_bytes=None,
max_bytes=None,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
The minimum number of bytes (inclusive). |
|
|
|
|
The maximum number of bytes (inclusive). |
At least one boundary must be set. The operator judges only by the raw byte length. Corrupted images that meet the size requirement still return True.
Return type: DataType.boolean().
from pyflink.multimodal.operators import image_file_size_filter
result = df.filter(
image_file_size_filter(
col("image_bytes"),
min_bytes=1024,
max_bytes=5 * 1024 * 1024
)
)