Image properties and filtering

Updated at:

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

is_valid_image

Determines whether an image passes the validity check.

Property extraction

image_metadata

Extracts the image width, height, number of channels, mode, and more.

image_aspect_ratio

Calculates the aspect ratio.

image_sharpness

Calculates image sharpness by using the Laplacian variance.

image_hash

Calculates the perceptual hash of an image.

Data filtering

image_size_filter

Filters by pixel dimensions.

image_shape_filter

Filters by aspect ratio.

image_file_size_filter

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

concurrency

Optional[int]

None

All operators on this page

The UDF concurrency. None uses the framework default.

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

mode

Optional[str]

None

The target mode. When set, the operator checks whether the input can be converted to this mode.

pixel_limit

Optional[int]

None

The maximum number of pixels (width * height).

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

max_edge

Optional[int]

None

The maximum length of the longest edge, in pixels, allowed before computation.None keeps the original size.

allow_upscale

bool

False

Whether to allow upscaling small images when max_edge is set.

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

method

str

"phash"

The hash algorithm. Supported values: "phash", "dhash", and "ahash".

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

min_w

Optional[int]

None

The minimum width (inclusive).

min_h

Optional[int]

None

The minimum height (inclusive).

max_w

Optional[int]

None

The maximum width (inclusive).

max_h

Optional[int]

None

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

min_ratio

Optional[float]

None

The minimum aspect ratio width / height (inclusive).

max_ratio

Optional[float]

None

The maximum aspect ratio width / height (inclusive).

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

min_bytes

Optional[int]

None

The minimum number of bytes (inclusive).

max_bytes

Optional[int]

None

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
    )
)