img2numpy is a Python SDK for converting image inputs into NumPy arrays inside your own source files.
from img2numpy import convert
array = convert("frame.png")
print(array.shape)
print(array.dtype)The primary product is the importable library. It also includes compatibility helpers and an optional FastAPI service for users who want HTTP conversion workflows.
pip install img2numpyfrom img2numpy import ConversionOptions, convert, convert_many
array = convert("images/cat.png")
options = ConversionOptions(
channel_mode="RGB",
normalize_mode="0..1",
dtype="float32",
resize=(224, 224),
)
normalized = convert("images/cat.webp", options=options)
batch = convert_many("images/*.jpg")Supported SDK inputs include:
- file paths and
PathLikeobjects - bytes
- file-like objects
- PIL images
- URLs
- directories, glob patterns, and iterables through
convert_many
Advanced SDK helpers:
convert_result: returns metadata-richConversionResultsave_npzandload_npz: write and read NumPy archives with manifest metadatasupported_formatsanddecoder_capabilities: inspect runtime decoder support
Primary output format is compressed NumPy archives (.npz).
- Browser UI:
GET /,POST / - Legacy API:
POST /api/convert - v1 command API:
POST /api/v1/{api_key}/{command} - Artifact download:
GET /api/v1/{api_key}/download/{artifact_id} - Async jobs:
POST /api/v1/{api_key}/jobs/submitGET /api/v1/{api_key}/jobs/{job_id}GET /api/v1/{api_key}/jobs/{job_id}/manifestGET /api/v1/{api_key}/jobs/{job_id}/stats
- Supported-format matrix:
GET /api/v1/formats
docker build -t img2numpy:latest .
docker run --rm -p 8585:8585 -e IMG2NUMPY_API_PORT=8585 img2numpy:latest- Browser UI:
http://127.0.0.1:8585 - API:
http://127.0.0.1:8585
bash ./img2numpy.sh /build
bash ./img2numpy.sh /build -a
bash ./img2numpy.sh /install -p 8000
bash ./img2numpy.sh /update -p 8000/build -aperforms multi-arch buildx push to172.16.120.5:5000/img2numpy:latest.-pcontrols the WebUI host port.- API host port is controlled by
IMG2NUMPY_API_PORT(default8585).
IMG2NUMPY_API_KEYS: comma-separated API keys for/api/v1/*(defaultdev-local-key)IMG2NUMPY_API_KEY_STORE_PATH: persistent API key profile store file (default<artifact_dir>/api_keys.json)IMG2NUMPY_API_PORT: API/listen port (default8585)IMG2NUMPY_ARTIFACT_DIR: output artifact directory (default./artifacts)IMG2NUMPY_ARTIFACT_TTL_SECONDS: artifact expiration TTL in seconds (default3600)IMG2NUMPY_MAX_UPLOAD_BYTES: max bytes per uploaded file (default52428800)IMG2NUMPY_MAX_BATCH_FILES: max files per batch call (default250)IMG2NUMPY_MAX_PROCESS_SECONDS: max processing time per batch/job (default120)IMG2NUMPY_MAX_ARTIFACT_BYTES: max total artifact store bytes (default1073741824)
POST / supports:
- multi-file uploads (
files, repeated) output_mode(npzdefault,linkoptional)dtypenormalize_mode(none,0..1,-1..1)channel_mode(RGB,RGBA,L, or auto)flattenmetadata_onlyfail_fast(applies in batch mode)
UI result cards include run summary, applied settings, per-file statuses, preview for first successful file, and artifact download link when generated.
The browser UI includes an API Key Profiles section to:
- generate one key per client app/profile
- persist keys on disk (
IMG2NUMPY_API_KEY_STORE_PATH) - revoke keys when a client should lose access
- inspect created/last-used timestamps per key
Use generated keys as {api_key} in v1 command routes.
POST /api/v1/{api_key}/{command}
Reserved commands:
convertbatchdownload
Unknown command returns 404.
output_mode:npz(default) orlinkdtypenormalize_mode:none,0..1,-1..1channel_mode:RGB,RGBA,Lflatten: booleanmetadata_only: boolean
- Required field:
file - Default response: downloadable
.npz output_mode=link: returns JSON withartifact_id+download_url
- Required field: one or more
files - Additional field:
fail_fast(falsedefault) - Mixed formats allowed in one request (decode-based validation)
- Per-file status metadata returned in link mode
- Field:
artifact_id(multipart form) - Returns artifact stream
GET /api/v1/{api_key}/download/{artifact_id}
POST /api/v1/{api_key}/jobs/submit
Multipart fields:
files(repeat for batch input)- all conversion fields from command API
metadata_json(optional JSON object)- supported shape:
global: metadata merged into all samplesper_file: filename-keyed metadata overrides
- supported shape:
callback_url(optional): completion callback target
Response:
job_idstatus(queued)status_urlmanifest_urlstats_url
GET /api/v1/{api_key}/jobs/{job_id}
Returns:
- lifecycle fields:
queued,running,done,failed - artifact metadata (
artifact_id,artifact_sha256, download URL) - callback state (
callback_url,callback_status) - links to manifest/stats endpoints
GET /api/v1/{api_key}/jobs/{job_id}/manifest
Includes:
schema_versionandconverter_version- per-sample
sample_id,artifact_key, status, shape/dtype - provenance (
source_filename,source_sha256,ingest_timestamp,job_id) - metadata passthrough values
- aggregate counts (
ok_count,error_count)
GET /api/v1/{api_key}/jobs/{job_id}/stats
Includes:
shape_distributionclass_countssplit_countsmissing_labels- valid/invalid/total sample counts
GET /api/v1/formats
Current matrix covers:
- PNG
- JPEG
- WEBP
- GIF
- BMP
- TIFF
.npzis the primary/default response format- link mode returns artifact references for deferred retrieval
- batch and job artifacts embed a
__manifest_json__entry - async jobs compute and report artifact
sha256 - artifact retention is TTL-based with background cleanup
export IMG2NUMPY_API_KEYS="key-one,key-two,key-three"Validation uses constant-time comparison.
Runtime behavior:
- API keys loaded from
IMG2NUMPY_API_KEY_STORE_PATHare authoritative. - On first start, keys from
IMG2NUMPY_API_KEYSseed the persistent store. - Key usage updates
last_used_attimestamps for profile tracking.
- Add new key to
IMG2NUMPY_API_KEYSalongside existing keys. - Restart service and migrate clients.
- Remove old key and restart again.
curl -X POST "http://127.0.0.1:8585/api/v1/dev-local-key/convert" \
-F "file=@/data/frame.png"curl -X POST "http://127.0.0.1:8585/api/v1/dev-local-key/batch" \
-F "files=@/data/a.png" \
-F "files=@/data/b.jpg" \
-F "files=@/data/c.webp" \
-F "output_mode=link" \
-F "fail_fast=false"curl -X POST "http://127.0.0.1:8585/api/v1/dev-local-key/jobs/submit" \
-F "files=@/data/a.png" \
-F "files=@/data/b.jpg" \
-F 'metadata_json={"global":{"split":"train"},"per_file":{"a.png":{"label":"cat"},"b.jpg":{"label":"dog"}}}'curl "http://127.0.0.1:8585/api/v1/dev-local-key/jobs/<job_id>"
curl "http://127.0.0.1:8585/api/v1/dev-local-key/jobs/<job_id>/manifest"
curl "http://127.0.0.1:8585/api/v1/dev-local-key/jobs/<job_id>/stats"curl -L "http://127.0.0.1:8585/api/v1/dev-local-key/download/<artifact_id>" -o output.npzImg2Numpy.py still provides:
img2numpyfolder2numpynpz2arraytuple2listsscrub_filename
pip install -r requirements.txt
pytest -q