Skip to content
项目

关于

纯 TypeScript 图像处理,可在任何环境中运行

设计与媒体

这些数字如何统计

可以做什么

  • WebP - ✓ - ✓ (lossless and lossy)
  • AVIF - ✓ - ✓ (lossless and lossy)
  • JPEG XL - ✓ - ✓ (lossless and lossy)
  • GIF, APNG (animations) - ✓ - ✓

实测星标增长

24小时
+2
7天
+3
15天
+10
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

ImageScript: Pure TypeScript image processing that runs everywhere

Documentation Playground Discussions Discord

npm JSR Tests MIT license


ImageScript decodes, edits and encodes images in pure TypeScript: no WebAssembly, no native binaries, no runtime dependencies. It runs on Node.js 22.13+, Deno, Bun, browsers and edge runtimes, and ships with TypeScript type definitions.

npm install imagescript              # Node.js, Bun and bundlers
deno add jsr:@matmen/imagescript     # Deno

Upgrading from 3.x? Read Migrating to 4.0 first.


Example

import { Image } from 'imagescript';
import { readFile, writeFile } from 'node:fs/promises';

const image = await Image.decode(await readFile('input.jpg'));
image.resize({ width: 256 }).roundCorners({ radius: 16 });
await writeFile('output.png', await image.encodePNG());

Example

The same code runs everywhere, only reading and writing the bytes differs. Image.decode() takes any ArrayBuffer or typed array (fetch(url).then(r => r.arrayBuffer()) in a browser or on an edge runtime, Deno.readFile(), Bun.file().arrayBuffer()), and the encoders return a Uint8Array. In a browser without a bundler, import it from jsDelivr:

import { Image } from 'https://cdn.jsdelivr.net/npm/imagescript@4/+esm';

Threads and bundlers

The codecs that can use worker threads (large AVIF and JPEG XL images, lossless WebP; the threads option) do so in Node.js, Deno and Bun, and in browsers on cross-origin isolated pages. Large PNGs are compressed in parallel on the zlib threads of Node.js (4 unless UV_THREADPOOL_SIZE says otherwise) and Bun instead, without a threads option. Where workers can't start, the work runs on the calling thread: the output is the same, only slower, and nothing warns about it. Browsers refuse workers from another origin, so the jsDelivr import above runs single-threaded; serve the package from your own origin (on a cross-origin isolated page) to get threads.

Bundlers that write one file (esbuild, Rollup, bun build) don't copy the worker's module: serve dist/util/worker.js next to the bundle, or the same single-threaded fallback is used. webpack and Vite emit it.


Features

Format Decode Encode
PNG ✓ ✓
JPEG ✓ ✓
WebP ✓ ✓ (lossless and lossy)
AVIF ✓ ✓ (lossless and lossy)
JPEG XL ✓ ✓ (lossless and lossy)
GIF, APNG (animations) ✓ ✓
BMP, QOI, TGA, Netpbm ✓ ✓
TIFF, ICO/CUR, Radiance HDR ✓
HDR gain maps (brighter highlights on HDR displays; JPEG, AVIF, JPEG XL) ✓ ✓

Animations can be decoded from GIF, APNG, WebP and JPEG XL, and encoded as GIF, APNG or WebP, all at once or frame by frame. Lossless encoding keeps every pixel exactly; lossy encoding makes smaller files by approximating some image detail. quality: 100 is lossless for WebP, AVIF and JPEG XL (JPEG has no lossless mode). Without a quality, WebP and JPEG XL are lossless and AVIF is lossy.

Not supported (decoding them throws an error): CCITT-compressed TIFF and arithmetic-coded JPEG. Lossless JPEG XL is fast but not small: for small lossless files, PNG or WebP are the better choice.

Embedded color profiles are converted to sRGB, the standard color format for most screens, where supported. CMYK and lookup-table profiles are left unchanged.

Supported image operations preserve gain maps; editing pixels directly with image.bitmap or setPixelAt() does not.

The documentation covers the full API. When upgrading from an older major version, see the changelog. bench/ compares ImageScript's speed and file sizes with Jimp, sharp and other libraries.


Credits

ImageScript builds on fflate, libjpeg, pdf.js, font-rs, mozjpeg, libwebp, HarfBuzz, dav1d, libavif, libaom, jxl-oxide and libjxl. The TrueType hinting follows FreeType: portions of this software are copyright © 2026 The FreeType Project (www.freetype.org). All rights reserved. See THIRD_PARTY_NOTICES.md for the licenses.

探索相关项目

加载中…