Operator ID: daft.las.functions.image.image_compress.ImageCompress
Image compression processor that supports multiple interpolation algorithms and various input and output formats.
Details | Caution and prerequisites |
|---|---|
Costs | Before calling an operator, you need to understand the model invocation costs associated with using the operator. For details, see Large model invocation billing. |
Authentication (API Key) | Before calling an operator, you need to generate an API Key for operator invocation. It is recommended to configure the API Key as an environment variable to ensure safer operator calls. For details, see Obtain and configure API Key. |
BaseURL | Before calling an operator, you need to determine the BaseURL for operator invocation based on the region where your current LAS service is deployed. This is used to configure the path parameter values for operator calls. |
Input column name | Description |
|---|---|
image | An array containing input images, supporting URL, Base64, and binary formats |
image_name | Optional parameter. An array containing image identifier names, used to generate output file names. |
An array of dictionaries containing processing results. Each element includes:
If a parameter does not have a default value, it is required.
Parameter name | Type | Default value | Description |
|---|---|---|---|
image_suffix | str | .jpg | Suffix of the output file. |
output_dir | str | "" | Output folder path for saving images. |
image_src_type | str | image_url | Format type of the input image. |
target_size | list | None | Size of the compressed image. |
quality | int | 85 | Compression quality parameter (adapted for different formats):
Default value: 85 |
method | str | lanczos | Interpolation algorithm used for scaling. |
The following code demonstrates how to use daft to run the operator for image compression.
from __future__ import annotations import os import daft from daft import col from daft.las.functions.image.image_compress import ImageCompress from daft.las.functions.udf import las_udf if __name__ == "__main__": # Content will be saved to the specified TOS path. Therefore, environment variables must be set to ensure write permissions to TOS, including: ACCESS_KEY, SECRET_KEY, TOS_ENDPOINT, TOS_REGION, TOS_TEST_DIR TOS_DIR = os.getenv("TOS_TEST_DIR", "tos_bucket") output_tos_dir = f"tos://{TOS_DIR}/image/image_compress" if os.getenv("DAFT_RUNNER", "native") == "ray": import logging import ray def configure_logging(): logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S.%s".format(), ) logging.getLogger("tracing.span").setLevel(logging.WARNING) logging.getLogger("daft_io.stats").setLevel(logging.WARNING) logging.getLogger("DaftStatisticsManager").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaScheduler").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaDispatcher").setLevel(logging.WARNING) ray.init(dashboard_host="0.0.0.0", runtime_env={"worker_process_setup_hook": configure_logging}) daft.set_runner_ray() daft.set_execution_config(actor_udf_ready_timeout=600) daft.set_execution_config(min_cpu_per_task=0) tos_dir_url = os.getenv("TOS_DIR_URL", "las-cn-beijing-public-online.tos-cn-beijing.volces.com") samples = { "image": [f"https://{tos_dir_url}/public/shared_image_dataset/cat.png"], "image_name": ["cat_ip_adapter"], } image_suffix = ".jpg" image_src_type = "image_url" target_size = [200, 200] quality = 85 method = "lanczos" local_dir = "" ds = daft.from_pydict(samples) ds = ds.with_column( "image_compress", las_udf( ImageCompress, construct_args={ "image_suffix": image_suffix, "output_dir": output_tos_dir, "image_src_type": image_src_type, "target_size": target_size, "quality": quality, "method": method, }, batch_size=1, )(col("image"), col("image_name")), ) ds.show() # ╭────────────────────────────────┬────────────────┬────────────────────────────────────────────╮ # │ image ┆ image_name ┆ image_compress │ # │ --- ┆ --- ┆ --- │ # │ String ┆ String ┆ Struct[base64: String, image_path: String] │ # ╞════════════════════════════════╪════════════════╪════════════════════════════════════════════╡ # │ https://las-ai-qa-online.tos-… ┆ cat_ip_adapter ┆ {base64: /9j/4AAQSkZJRgABAQAA… │ # ╰────────────────────────────────┴────────────────┴────────────────────────────────────────────╯