You need to enable JavaScript to run this app.
Lake AI Service

Lake AI Service

Copy page
Download PDF
Image processing
Image hash value
Copy page
Download PDF
Image hash value

Operator introduction

Description

Image hash calculation operator: Supports three input formats—URL, Base64, and binary—and outputs both hexadecimal and binary hashes in a unified manner.

Key features

  • Supports five hash methods: ahash, dhash, phash, whash, md5
  • Selectable input types: image_url, image_base64, image_binary
  • Supports batch processing

Applicable scenarios

  • Image deduplication and recall of near-duplicate images
  • File consistency verification
  • Multi-format hash value output

Caution

  • The input data type must match the image_src_type parameter
  • The output of md5 and perceptual hash will be standardized to 64-bit binary and 16-digit hexadecimal
  • The operator depends on imagededup.methods; if the dependency is unavailable, an empty string will be returned
  • Images are uniformly read and converted to RGB format using Pillow

Supported hash methods

  • ahash
  • dhash
  • phash
  • whash
  • md5

Daft invocation

Operator parameters

Input

Input column name

Note

image

An array containing image data, with element types as string or binary (bytes). The specific meaning is determined by the image_src_type parameter.

Output

A structured result array, where each element contains the following fields:

  • hash_hex (str): The hexadecimal hash value of the image. For the md5 method, this value is 16 characters (the first 64 bits of the full digest).
  • hash_bin (str): The binary hash value of the image. For the md5 method, this value is 64 bits.

Parameters

If a parameter does not have a default value, it is required

Parameter name

Type

Default value

Description

image_src_type

str

"image_url"

The source type of the input image. Selectable values: ["image_url", "image_base64", "image_binary"].

method

str

"phash"

Hash calculation method. Selectable values: {"ahash", "dhash", "phash", "whash", "md5"}. md5 is calculated using hashlib.md5, while other methods use the imagededup library.

batch_size

int

64

Batch size, used to balance throughput and memory consumption.

Examples

The following code demonstrates how to use daft to invoke the ImageHash operator to compute the perceptual hash value of an image.

from __future__ import annotations

import os

import daft
from daft import col
from daft.las.functions.image.image_hash import ImageHash
from daft.las.functions.udf import las_udf

if __name__ == "__main__":
    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_ip_adapter.jpeg"
        ]
    }

    ds = daft.from_pydict(samples)
    ds = ds.with_column(
        "image_hash",
        las_udf(
            ImageHash,
            construct_args={
                "image_src_type": "image_url",
                "method": "phash",
            },
            num_gpus=0,
            batch_size=1,
            concurrency=1,
        )(col("image")),
    )

    ds.show()

    # ╭────────────────────────────────┬────────────────────────────────────────╮
    # │ image                          ┆ image_hash                             │
    # │ ---                            ┆ ---                                    │
    # │ Utf8                           ┆ Struct[hash_hex: Utf8, hash_bin: Utf8] │
    # ╞════════════════════════════════╪════════════════════════════════════════╡
    # │ https://las-cn-beijing-public… ┆ {hash_hex: 8d3986a636e768ad,           │
    # │                                ┆ …                                      │
    # ╰────────────────────────────────┴────────────────────────────────────────╯
Last updated: 2026.05.12 19:06:36