Image transformation and codec

Updated at:

Functions for image encoding and decoding, format conversion, resizing, and pixel processing.

The functions in this topic are provided by the Python multimodal function library (VVR 11.8+). Load the module at the beginning of the SQL job before use:

LOAD MODULE python;
USE MODULES python, core;

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

Function summary

Function

Description

IMAGE_DECODE

Decodes raw image bytes into IMAGE

IMAGE_ENCODE

Encodes a decoded image into compressed bytes or a Data URL

IMAGE_COMPRESS

Recompresses encoded image bytes

IMAGE_CONVERT_FORMAT

encodingformat

IMAGE_CONVERT_MODE

imagemode

IMAGE_RESIZE

size

IMAGE_RESCALE

Scale proportionally

IMAGE_CROP

image

IMAGE_CROP_BLACK_BORDER

Detect and crop black borders

IMAGE_FLIP

image

IMAGE_BLUR

processing

IMAGE_ADJUST_COLOR

, and and

IMAGE_REMOVE_BACKGROUND

image

IMAGE_DECODE

Decodes raw image bytes into IMAGE.

IMAGE_DECODE(image_bytes [, on_error [, mode [, pixel_limit]]])

Parameter

Type

Default

Description

image_bytes

BYTES

Required

encoding imagebytes.

on_error

STRING NOT NULL

'null'

image errorprocessing. 'null' Returns NULL; 'raise' exception.

mode

STRING

NULL

mode. NULL mode, RGB or RGBA.

pixel_limit

INT

NULL

pixel, pixel image on_error processing.

Return type: IMAGE

SELECT IMAGE_METADATA(IMAGE_DECODE(img_bytes, 'null', 'RGB')).mode
FROM raw_images;

IMAGE_ENCODE

decoding Image encoding compressionbytes, or encoding Base64 Data URL string.

IMAGE_ENCODE(image_input [, format [, quality [, output]]])

Parameter

Type

Default

Description

image_input

IMAGE

Required

The decoded image.

format

STRING

NULL

format ('JPEG', 'PNG'). NULL JPEG encoding.

Note

JPEG format, encoding RGBA image JPEG RGB.

quality

INT NOT NULL

85

compression, [1, 100], JPEG, WEBP format.

output

STRING NOT NULL

'bytes'

output. 'bytes' Returnsencoding imagebytes; 'data_url' Returns data:image/...;base64,... string.

Return type: BYTES or STRING.

SELECT
 IMAGE_ENCODE(IMAGE_DECODE(img_bytes), 'PNG') AS png_bytes,
 IMAGE_ENCODE(IMAGE_DECODE(img_bytes), 'PNG', 85, 'data_url') AS png_url
FROM raw_images;

IMAGE_COMPRESS

Recompresses encoded image bytes into a specified format and quality.

IMAGE_COMPRESS(image_bytes [, quality [, format]])

Parameter

Type

Default

Description

image_bytes

BYTES

Required

encoding imagebytes.

quality

INT NOT NULL

85

compression, [1, 100], JPEG, WEBP format.

format

STRING NOT NULL

'JPEG'

outputformat ('JPEG', 'PNG').

Return type: BYTES

SELECT IMAGE_COMPRESS(img_bytes, 60, 'JPEG') FROM raw_images;

IMAGE_CONVERT_FORMAT

Converts encoded image bytes into a different encoding format.

IMAGE_CONVERT_FORMAT(image_bytes, output_format)

Parameter

Type

Default

Description

image_bytes

BYTES

Required

encoding imagebytes.

output_format

STRING NOT NULL

Required

format ('JPEG', 'PNG').

Return type: BYTES

SELECT IMAGE_CONVERT_FORMAT(img_bytes, 'JPEG') FROM raw_images;

IMAGE_CONVERT_MODE

image mode.

IMAGE_CONVERT_MODE(image_input, mode)

Parameter

Type

Default

Description

image_input

IMAGE

Required

The decoded image.

mode

STRING NOT NULL

Required

mode.

Return type: IMAGE

SELECT IMAGE_METADATA(IMAGE_CONVERT_MODE(IMAGE_DECODE(img_bytes), 'L')).channels
FROM raw_images;

IMAGE_RESIZE

image size.

IMAGE_RESIZE(image_input, width, height [, method])

Parameter

Type

Default

Description

image_input

IMAGE

Required

The decoded image.

width

INT NOT NULL

Required

width (pixel).

height

INT NOT NULL

Required

height (pixel).

method

STRING NOT NULL

'lanczos'

: 'lanczos', 'bilinear', 'nearest', 'bicubic'.

Return type: IMAGE

SELECT IMAGE_ENCODE(IMAGE_RESIZE(IMAGE_DECODE(img_bytes), 512, 512), 'JPEG', 85)
FROM raw_images;

IMAGE_RESCALE

image.

IMAGE_RESCALE(image_input, scale [, method])

Parameter

Type

Default

Description

image_input

IMAGE

Required

The decoded image.

scale

DOUBLE NOT NULL

Required

, 0.5.

method

STRING NOT NULL

'lanczos'

: 'lanczos', 'bilinear', 'nearest', 'bicubic'.

Return type: IMAGE

SELECT IMAGE_METADATA(IMAGE_RESCALE(IMAGE_DECODE(img_bytes), 0.5)).width FROM raw_images;

IMAGE_CROP

image.

IMAGE_CROP(image_input [, crop_coords [, crop_type [, crop_ratio [, crop_size]]]])

Parameter

Type

Default

Description

image_input

IMAGE

Required

The decoded image.

crop_coords

ARRAY<INT>

NULL

pixel [left, top, right, bottom], 'coordinate'.

crop_type

STRING NOT NULL

'center'

. 'coordinate' crop_coords; 'ratio' crop_ratio; 'center' crop_size, crop_size NULL crop_ratio.

crop_ratio

ARRAY<DOUBLE> NOT NULL

ARRAY[0.8, 0.8]

[width_ratio, height_ratio].

Note

If you configure the crop_size parameter, you must pass in an array of valid ratios as the fallback strategy.

crop_size

ARRAY<INT>

NULL

outputsize [width, height].

Return type: IMAGE

For coordinate-based cropping, and for center cropping whose output size is larger than the original image, areas outside the original image are filled with zero-value pixels. Ratio-based cropping never exceeds the original image.

SELECT IMAGE_CROP(img, ARRAY[10, 10, 210, 210]) FROM images;

SELECT IMAGE_CROP(
 img,
 CAST(NULL AS ARRAY<INT>),
 'ratio',
 ARRAY[0.5, 0.5]
) FROM images;

IMAGE_CROP_BLACK_BORDER

image.

IMAGE_CROP_BLACK_BORDER(image_input [, detect_algorithm [, black_threshold
 [, edge_sensitivity [, min_border_size]]]])

Parameter

Type

Default

Description

image_input

IMAGE

Required

The decoded image.

detect_algorithm

STRING NOT NULL

'auto'

algorithm: 'auto', 'threshold', 'histogram', 'edge'. 'auto',.

black_threshold

INT NOT NULL

10

threshold, pixel value.

edge_sensitivity

DOUBLE NOT NULL

1.0

The edge detection sensitivity.

min_border_size

INT NOT NULL

1

pixelwidth.

Return type: IMAGE

SELECT IMAGE_CROP_BLACK_BORDER(img, 'threshold', 10) FROM images;

IMAGE_FLIP

image.

IMAGE_FLIP(image_input [, mode])

Parameter

Type

Default

Description

image_input

IMAGE

Required

The decoded image.

mode

STRING NOT NULL

'horizontal'

: 'horizontal', 'vertical' or 'rotate180'.

Return type: IMAGE

SELECT IMAGE_FLIP(img, 'vertical') FROM images;

IMAGE_BLUR

image processing.

IMAGE_BLUR(image_input [, radius [, blur_type]])

Parameter

Type

Default

Description

image_input

IMAGE

Required

The decoded image.

radius

INT NOT NULL

2

The blur radius must be a non-negative number. This parameter does not take effect in mean mode.

blur_type

STRING NOT NULL

'gaussian'

algorithm:

  • 'gaussian' ()

  • 'box':

  • 'mean' value.

Return type: IMAGE

SELECT IMAGE_ENCODE(IMAGE_BLUR(img, 5), 'PNG') FROM images;

IMAGE_ADJUST_COLOR

, and and.

IMAGE_ADJUST_COLOR(image_input [, brightness [, contrast [, saturation]]])

Parameter

Type

Default

Description

image_input

IMAGE

Required

The decoded image.

brightness

DOUBLE NOT NULL

1.0

, value 0. 1.0.

contrast

DOUBLE NOT NULL

1.0

, value 0. 1.0.

saturation

DOUBLE NOT NULL

1.0

and, value 0. 1.0.

Return type: IMAGE

For images in L16 mode, the adjustment factor parameter is not supported.

SELECT IMAGE_ADJUST_COLOR(img, 1.2, 1.0, 1.0) FROM images;

IMAGE_REMOVE_BACKGROUND

image, Returns or image.

IMAGE_REMOVE_BACKGROUND(image_input [, alpha_matting
 [, alpha_matting_foreground_threshold
 [, alpha_matting_background_threshold
 [, alpha_matting_erode_size [, bgcolor]]]]])

Parameter

Type

Default

Description

image_input

IMAGE

Required

The decoded image.

alpha_matting

BOOLEAN NOT NULL

FALSE

alpha matting,.

alpha_matting_foreground_threshold

INT NOT NULL

240

Alpha matting threshold, valuerange [0, 255].

alpha_matting_background_threshold

INT NOT NULL

10

Alpha matting threshold, valuerange [0, 255].

alpha_matting_erode_size

INT NOT NULL

10

Alpha matting size, integer.

bgcolor

ARRAY<INT>

NULL

RGBA, ARRAY[255, 255, 255, 255]. valuerange[0, 255]NULL.

Return type: IMAGE

SELECT IMAGE_REMOVE_BACKGROUND(img, FALSE, 240, 10, 10, ARRAY[255, 255, 255, 255])
FROM images;