Image properties and filtering

Updated at:

Functions for image validity verification, metadata extraction, sharpness calculation, perceptual hashing, and size filtering.

The functions in this topic are provided by the Python multimodal function library (VVR 11.8 or later). Before you use them, add the following statements at the beginning of the SQL deployment:

LOAD MODULE python;
USE MODULES python, core;

For complete examples and the IMAGE type, see Image function overview.

Function list

Function

Description

IS_VALID_IMAGE

Determines whether an image passes the validity check

IMAGE_METADATA

Extracts the image width, height, number of channels, mode, and format

IMAGE_ASPECT_RATIO

Calculates the aspect ratio

IMAGE_SHARPNESS

Calculates sharpness by using the Laplacian variance

IMAGE_HASH

Calculates the perceptual hash value

IMAGE_SIZE_FILTER

Filters by pixel size

IMAGE_SHAPE_FILTER

Filters by aspect ratio

IMAGE_FILE_SIZE_FILTER

Filters by encoded file size

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.

IS_VALID_IMAGE(image_input [, mode [, pixel_limit]])

Parameter

Type

Default value

Description

image_input

IMAGE / BYTES

Required

The decoded image or the encoded image bytes.

mode

STRING

NULL

Verifies whether the image can be converted to the target mode. NULL indicates that the target mode is not checked.

pixel_limit

INT

NULL

An image that exceeds this number of pixels is considered invalid. NULL indicates that no user-defined pixel limit is set.

Return type: BOOLEAN NOT NULL

NULL, corrupted images, and inputs that exceed the pixel limit return FALSE.

SELECT * FROM images
WHERE IS_VALID_IMAGE(img, 'RGB', 67108864);

IMAGE_METADATA

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

IMAGE_METADATA(image_input)

Input type: IMAGE / BYTES

Return type: ROW<width INT, height INT, channels INT, mode STRING, format STRING>

NULL and inputs whose metadata cannot be read return NULL.

Field

Description

width

The image width, in pixels.

height

The image height, in pixels.

channels

The number of image channels.

mode

The image mode. For a decoded IMAGE input, the mode of the decoded image is returned. For an encoded bytes input, the mode name in the original image container is returned.

format

The encoding container format, such as 'JPEG', 'PNG', 'WEBP'. A decoded IMAGE returns 'UNKNOWN'.

SELECT
  IMAGE_METADATA(img).width,
  IMAGE_METADATA(img).height,
  IMAGE_METADATA(img).mode,
  IMAGE_METADATA(img).format
FROM images;

IMAGE_ASPECT_RATIO

Calculates the aspect ratio, that is, width / height.

IMAGE_ASPECT_RATIO(image_input)

Input type: IMAGE / BYTES

Return type: DOUBLE

NULL, invalid images, and inputs whose size cannot be read return NULL.

SELECT IMAGE_ASPECT_RATIO(img) FROM images;

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.

IMAGE_SHARPNESS(image_input [, max_edge [, allow_upscale]])

Parameter

Type

Default value

Description

image_input

IMAGE

Required

The decoded image.

max_edge

INT

NULL

Scales the longest side to this value before calculation. NULL indicates that the original size is used.

allow_upscale

BOOLEAN

FALSE

Specifies whether to allow upscaling when max_edge exceeds the image size. NULL behaves the same as FALSE and does not upscale small images.

Return type: DOUBLE, the raw sharpness metric. Value range: [0, +∞). The value is not normalized.

NULL input returns NULL.

Unlike IMAGE_QUALITY_SCORE, IMAGE_SHARPNESS measures only sharpness. The threshold is affected by image size, texture, contrast, and noise. To compare across images, set a fixed max_edge.

SELECT * FROM images
WHERE IMAGE_SHARPNESS(img, 512, FALSE) > 100;

IMAGE_HASH

Calculates the perceptual hash value of an image.

IMAGE_HASH(image_input [, method])

Parameter

Type

Default value

Description

image_input

IMAGE

Required

The decoded image.

method

STRING NOT NULL

'phash'

The hash algorithm. Supported values: 'phash', 'dhash', 'ahash'.

Return type: STRING, a hash value encoded in hexadecimal.

SELECT IMAGE_HASH(img, 'dhash') FROM images;

IMAGE_SIZE_FILTER

Filters images by pixel size.

IMAGE_SIZE_FILTER(image_input, min_w [, min_h [, max_w [, max_h]]])

Parameter

Type

Default value

Description

image_input

IMAGE / BYTES

Required

The decoded image or the encoded image bytes.

min_w

INT

NULL

The minimum width. NULL indicates that no minimum width is set.

min_h

INT

NULL

The minimum height. NULL indicates that no minimum height is set.

max_w

INT

NULL

The maximum width. NULL indicates that no maximum width is set.

max_h

INT

NULL

The maximum height. NULL indicates that no maximum height is set.

Return type: BOOLEAN NOT NULL

Returns TRUE when the image satisfies all configured size constraints. NULL and invalid inputs return FALSE. At least one size boundary must be set.

SELECT * FROM images
WHERE IMAGE_SIZE_FILTER(img, 256, 256, 4096, 4096);

IMAGE_SHAPE_FILTER

Filters images by aspect ratio.

IMAGE_SHAPE_FILTER(image_input, min_ratio [, max_ratio])

Parameter

Type

Default value

Description

image_input

IMAGE / BYTES

Required

The decoded image or the encoded image bytes.

min_ratio

DOUBLE

NULL

The minimum aspect ratio. NULL indicates that no minimum aspect ratio is set.

max_ratio

DOUBLE

NULL

The maximum aspect ratio. NULL indicates that no maximum aspect ratio is set.

Return type: BOOLEAN NOT NULL

Returns TRUE when the image aspect ratio satisfies all configured constraints. NULL, invalid inputs, and inputs whose size cannot be read return FALSE.

SELECT * FROM images
WHERE IMAGE_SHAPE_FILTER(img, 0.5, 2.0);

IMAGE_FILE_SIZE_FILTER

Filters images by encoded file size. This function accepts only encoded bytes, not IMAGE.

IMAGE_FILE_SIZE_FILTER(image_bytes, min_bytes [, max_bytes])

Parameter

Type

Default value

Description

image_bytes

BYTES

Required

The encoded image bytes.

min_bytes

INT

NULL

The minimum encoded size. NULL indicates that no minimum size is set.

max_bytes

INT

NULL

The maximum encoded size. NULL indicates that no maximum size is set.

Return type: BOOLEAN NOT NULL

Returns TRUE when the encoded byte size satisfies all configured constraints. NULL input returns FALSE.

SELECT * FROM raw_images
WHERE IMAGE_FILE_SIZE_FILTER(img_bytes, 1024, 10 * 1024 * 1024);