daft.functions.image_hash#
image_hash #
image_hash(image: Expression, *, method: Literal['phash', 'phash_simple', 'dhash', 'dhash_vertical', 'ahash', 'whash', 'crop_resistant', 'colorhash'] = 'phash', hash_size: int = 8, binbits: int = 3, segments: int = 3) -> Expression
Compute a perceptual hash of an image column for near-duplicate detection.
Returns a FixedSizeBinary column.
Output size by method:
- Single-segment methods:
hash_size * hash_sizebits. "crop_resistant":segments * segments * hash_size * hash_sizebits."colorhash":14 * binbitsbits (14 colour/intensity bins).
Two hashes with a low Hamming distance indicate visually similar images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image | Image Expression | image column to hash. | required |
method | str, default="phash" | Hash algorithm to use. One of:
| 'phash' |
hash_size | int, default=8 | Grid size for spatial hash methods. The output has | 8 |
binbits | int, default=3 | Bits per bin for | 3 |
segments | int, default=3 | Grid dimension for | 3 |
Returns:
| Name | Type | Description |
|---|---|---|
Expression | FixedSizeBinary Expression | Hash bytes for each image. |
Example
import daft from daft.functions import image_hash df = daft.from_pydict({"img": [...]}) # doctest: +SKIP df = df.with_column("hash", image_hash(df["img"], method="phash")) # doctest: +SKIP
crop-resistant hash with a 4×4 grid#
df = df.with_column("ch", image_hash(df["img"], method="crop_resistant", segments=4)) # doctest: +SKIP
colour-distribution hash#
df = df.with_column("chash", image_hash(df["img"], method="colorhash")) # doctest: +SKIP
Source code in daft/functions/image.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |