Skip to main content

Image writer

Take decoded pixels back to a file or upload body.

ImageWriter encodes ImageBitmap, Image, and image-data values into formats such as PNG and JPEG. The writer owns encoding while the application decides where the resulting bytes go.

The image writing path

Start with pixels. Choose the encoded representation.

The writer accepts the common image values produced by loaders.gl and uses the runtime’s available canvas or image encoder to produce uploadable bytes.

Input

ImageBitmap, Image, or image-data object

Format

PNG, JPEG, and environment-supported types

Quality

Optional JPEG or WebP quality control

Output

Encoded ArrayBuffer for saving or upload

The ImageWriter class can encode an image into ArrayBuffer both under browser and Node.js

Detailed guide

Encoding and runtime details

The reference below covers accepted image values, output MIME types, quality options, browser support, and Node.js behavior.

ImageWriter
File Extension.png, .jpg, .jpeg
File TypeBinary
Data FormatImageBitmap, Image or "image data"
File FormatJPEG, PNG, ...
Encoder TypeAsynchronous
Worker Thread❌ No (but may run on separate native thread in browsers)
Streaming❌ No

Usage​

import {ImageWriter} from '@loaders.gl/images/image/bundled';
import {encode} from '@loaders.gl/core';

const arrayBuffer = await encode(imageData, ImageWriter, {
image: {mimeType: 'image/png'}
});

imageData is an already decoded image. In Node.js, import @loaders.gl/core/polyfills before using the image adapter and save the resulting bytes with your filesystem API.

Data Format​

The ImageWriter can encode three different in-memory Image representations into binary image representation (such as JPEG or PNG images) that can then be saved or uploaded.

The supported image types are:

  • ImageBitmap- Optimized image class on modern browsers.
  • Image - Works on all browsers, less performant.
  • "image data object" - an ImageData like object that hold the raw decoded bytes of an image. Works in both browsers and Node.js.

Options​

OptionTypeDefaultDescription
image.mimeTypestring'image/png'Encoded output MIME type supported by the runtime.
image.jpegQualitynumber | nullnullQuality between 0 and 1 for JPEG/WebP; null uses the runtime default.

Remarks​

  • Supported image types (MIME types) depend on the browser / environment. As a rule, image/png and image/jpeg are always supported. (Unfortunately it is not currently clear how to determine what formats are available on any given browser, other than "trial and error").
  • jpegQuality argument is not supported on all platforms (in which case it is assumed to have reasonable but platform-dependent defaults).
  • Browser encoding uses OffscreenCanvas.convertToBlob() when available and falls back to canvas.toBlob() on the page. Worker encoding requires OffscreenCanvas with a 2D rendering context.