Skip to content
1 min read · 193 words

@nhtio/adk/batteries/generation/transformers_js/helpers

Env-branched PNG-encoding helper for transformers.js Generation's RawImage results.

Remarks

generate_images() resolves an array of RawImage instances (raw decoded pixel data, not encoded bytes). Encoding those to PNG is environment-branched in the installed @huggingface/transformers source (src/utils/image.js): browser/web-worker → await img.toBlob('image/png') (throws outside a web env); Node/other → img.toSharp().png().toBuffer() (its toSharp() throws inside a web env). Mirrors RawImage.save()'s own branch (apis.IS_WEB_ENV there) — but this module cannot import apis itself: @huggingface/transformers's package root only re-exports env/LogLevel from env.js (verified against src/transformers.js's export list), not apis. A local, structurally-equivalent environment check (typeof window !== 'undefined' && typeof window.document !== 'undefined', matching IS_BROWSER_ENV's own definition in env.js) stands in for it, with a try/catch fallback to the other branch as a safety net for exotic runtimes (e.g. a web worker, which has no window but is still a "web env" per apis.IS_WEB_ENV).

Interfaces

InterfaceDescription
RawImageLikeThe exact toBlob/toSharp surface this helper reads off a resolved RawImage-like value.

Functions

FunctionDescription
rawImageToEncodedBytesEncodes a resolved RawImage-like value to PNG bytes.