Image properties and filtering
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 |
|
Determines whether an image passes the validity check |
|
|
Extracts the image width, height, number of channels, mode, and format |
|
|
Calculates the aspect ratio |
|
|
Calculates sharpness by using the Laplacian variance |
|
|
Calculates the perceptual hash value |
|
|
Filters by pixel size |
|
|
Filters by aspect ratio |
|
|
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 |
|
Required |
The decoded image or the encoded image bytes. |
|
mode |
|
|
Verifies whether the image can be converted to the target mode. |
|
pixel_limit |
|
|
An image that exceeds this number of pixels is considered invalid. |
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 |
|
|
The image width, in pixels. |
|
|
The image height, in pixels. |
|
|
The number of image channels. |
|
|
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. |
|
|
The encoding container format, such as |
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 |
|
Required |
The decoded image. |
|
max_edge |
|
|
Scales the longest side to this value before calculation. |
|
allow_upscale |
|
|
Specifies whether to allow upscaling when max_edge exceeds the image size. |
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 |
|
Required |
The decoded image. |
|
method |
|
|
The hash algorithm. Supported values: |
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 |
|
Required |
The decoded image or the encoded image bytes. |
|
min_w |
|
|
The minimum width. |
|
min_h |
|
|
The minimum height. |
|
max_w |
|
|
The maximum width. |
|
max_h |
|
|
The maximum height. |
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 |
|
Required |
The decoded image or the encoded image bytes. |
|
min_ratio |
|
|
The minimum aspect ratio. |
|
max_ratio |
|
|
The maximum aspect ratio. |
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 |
|
Required |
The encoded image bytes. |
|
min_bytes |
|
|
The minimum encoded size. |
|
max_bytes |
|
|
The maximum encoded size. |
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);