Skip to main content

imgcache-client

Python client for the imgcache REST API — a centralized image cache with perceptual hashing and duplicate detection.

Installation

Install directly from the local package:

pip install ./client

Or add it to a requirements.txt:

dwilson-imgcache-client>=0.2.0

Requires: Python 3.11+, httpx>=0.27.0


Quick start

from imgcache_client import ImgCacheClient

client = ImgCacheClient("http://localhost:8010")

# Store an image
with open("photo.jpg", "rb") as f:
    entry = client.store(url="https://example.com/photo.jpg", file_bytes=f.read(), client_name="my_scraper")

print(entry["content_hash"])   # BLAKE2b hash / storage key
print(entry["perceptual_hash"]) # pHash for similarity search

# Retrieve raw bytes
img_bytes = client.get_bytes(entry["content_hash"])

# Retrieve metadata only
meta = client.get_meta(entry["content_hash"])

# Look up by source URL
meta = client.lookup("https://example.com/photo.jpg")

# Search by URL substring
results = client.search(url_contains="example.com")

# Find visually similar images
similar = client.similar(perceptual_hash=entry["perceptual_hash"], max_hamming_distance=4)

# Delete
client.delete(entry["content_hash"])

client.close()  # or use as a context manager (see below)

Use as a context manager to close the HTTP connection automatically:

with ImgCacheClient("http://localhost:8010") as client:
    entry = client.store(url=url, file_bytes=img_bytes, client_name="my_scraper")

API reference

ImgCacheClient(base_url, timeout=30.0)

Parameter Type Description
base_url str Base URL of the imgcache service, e.g. http://localhost:8010
timeout float Request timeout in seconds (default 30.0)

store(url, file_bytes, client_name, lookup_time=None, filename=None) → dict

Store an image in the cache.

Parameter Type Description
url str Source URL of the image
file_bytes bytes Raw binary image data
client_name str Identifier for the scraper submitting the image
lookup_time datetime (optional) When the image was fetched from origin; defaults to utcnow()
filename str (optional) Original filename hint (used for Content-Type detection)

Returns the full entry metadata dict. HTTP 201 means newly stored; HTTP 200 means an identical image already existed.


get_bytes(content_hash) → bytes

Download the raw binary content of a stored image by its BLAKE2b content hash.


get_meta(content_hash) → dict | None

Retrieve metadata for a stored image without downloading the binary. Returns None if not found.


lookup(url) → dict | None

Retrieve metadata for the most recent cached entry matching an exact source URL. Returns None if not found.


search(url_contains) → list[dict]

Return metadata for all cached entries whose source URL contains the given substring. Useful for finding all cached variants of a URL.

# Matches https://example.com/products/img1.jpg, /img2.jpg, etc.
results = client.search(url_contains="example.com/products")
for entry in results:
    print(entry["url"], entry["content_hash"])

similar(perceptual_hash, max_hamming_distance=4) → list[dict]

Find visually similar images by comparing perceptual hashes. max_hamming_distance controls how strict the match is — lower values mean more similar results.

similar = client.similar(perceptual_hash="f8c0e0e0f0e0c080", max_hamming_distance=4)

delete(content_hash) → None

Delete a cached image and its associated storage file by content hash.


health() → dict

Check that the service is reachable. Returns {"status": "ok"} when healthy.


close() → None

Close the underlying HTTP connection. Called automatically when used as a context manager.


Entry schema

Fields returned by store, get_meta, lookup, search, and similar:

Field Type Description
url str Source URL the image was fetched from
content_hash str BLAKE2b hash of the raw image bytes (storage key)
content_type str MIME type, e.g. image/jpeg
file_size_bytes int Size of the stored image in bytes
original_filename str | null Filename hint supplied at store time
width int | null Image width in pixels
height int | null Image height in pixels
perceptual_hash str | null pHash for similarity comparisons
client_name str Scraper that submitted the entry
lookup_time str (ISO 8601) When the image was fetched from origin
created_at str (ISO 8601) When the entry was stored in the cache

get_bytes returns raw bytes rather than a metadata dict.

Metadata

Release files for dwilson-imgcache-client 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dwilson-imgcache-client 0.3.0
File Size Uploaded Provenance Actions
dwilson_imgcache_client-0.3.0.tar.gz 3.7 kB Without provenance Details for dwilson_imgcache_client-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dwilson-imgcache-client 0.3.0
File Interpreter ABI Platform Actions
dwilson_imgcache_client-0.3.0-py3-none-any.whl Python 3 none any Details for dwilson_imgcache_client-0.3.0-py3-none-any.whl

Total release size: 7.6 kB

Release files / dwilson_imgcache_client-0.3.0.tar.gz

Download URL dwilson_imgcache_client-0.3.0.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4f882c6e386167fcd8dd447c3a082a5c75a2d3aaf90a327948964a212f39031d
BLAKE2b-256 checksum
How to use checksums
1ed89ad108307a67277bd393b902666f89ae61f9441a72bc1e8aa255284a46f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / dwilson_imgcache_client-0.3.0-py3-none-any.whl

Download URL dwilson_imgcache_client-0.3.0-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ce74ae0f2c13724ab0fdaf58932261ee40d978bd25ff096f21af16a4dfa7a0c3
BLAKE2b-256 checksum
How to use checksums
96a6ee421153a3ba10c10f7ea9a1621e097d1634293ef1a239185ffadbf8060b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Security for dwilson-imgcache-client 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page