Repository navigation
docs(packages/js): add README for npm package #466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Ryan-Millard
merged 10 commits into
Ryan-Millard:dev
from
Prachi-Gupta2808:docs/js-package-readme
Jul 2, 2026
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7c20c27
docs(packages/js): add README for npm package
Prachi-Gupta2808 320d811
Remove test instructions from README.md
Prachi-Gupta2808 deeee0a
did required changes
Prachi-Gupta2808 a3d78d7
fixed CDN badges and inline
Prachi-Gupta2808 dacebab
trying to fix alignment
Prachi-Gupta2808 dd42fe8
footer alignment fix
Prachi-Gupta2808 48b1998
did required changes
Prachi-Gupta2808 65641e9
Enhance README with before/after examples and features
Prachi-Gupta2808 15aa374
Revise README structure and image references
Prachi-Gupta2808 321a9cf
Merge branch 'main' into docs/js-package-readme
Ryan-Millard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| # img2num | ||
|
|
||
| Img2Num is a raster vectorization library — it converts PNG/JPEG images to clean, layered SVG paths. | ||
|
|
||
| [](https://www.npmjs.com/package/img2num) | ||
| [](https://github.com/Ryan-Millard/Img2Num/blob/main/LICENSE) | ||
|
Prachi-Gupta2808 marked this conversation as resolved.
Resolved
|
||
|
|
||
| ## Supported Runtimes | ||
|
|
||
| - **Browsers** — via Web Workers | ||
| - **Node.js** — supported (`node >= 14`), uses `worker_threads` with WebGPU via Dawn | ||
|
Prachi-Gupta2808 marked this conversation as resolved.
Resolved
|
||
|
|
||
| > **ESM only.** This package uses `"type": "module"` and has no CommonJS entry point. | ||
|
Prachi-Gupta2808 marked this conversation as resolved.
Resolved
|
||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install img2num | ||
| ``` | ||
|
|
||
| ```bash | ||
| pnpm add img2num | ||
| ``` | ||
|
|
||
| ```bash | ||
| yarn add img2num | ||
| ``` | ||
|
|
||
| ```bash | ||
| bun add img2num | ||
| ``` | ||
|
|
||
|
Prachi-Gupta2808 marked this conversation as resolved.
Resolved
|
||
| ## Quick Start | ||
|
|
||
| ### All-in-one (recommended) | ||
|
|
||
| ```js | ||
| import { imageToUint8ClampedArray, imageToSvg } from "img2num"; | ||
|
|
||
| const { pixels, width, height } = await imageToUint8ClampedArray(file); | ||
| const { svg } = await imageToSvg({ pixels, width, height }); | ||
|
|
||
| console.log(svg); // SVG string | ||
| ``` | ||
|
Prachi-Gupta2808 marked this conversation as resolved.
Resolved
|
||
|
|
||
| ### Step-by-step pipeline | ||
|
|
||
| ```js | ||
| import { | ||
| imageToUint8ClampedArray, | ||
| bilateralFilter, | ||
| kmeans, | ||
| findContours, | ||
| } from "img2num"; | ||
|
|
||
| const { pixels, width, height } = await imageToUint8ClampedArray(file); | ||
|
|
||
| const filtered = await bilateralFilter({ pixels, width, height }); | ||
|
|
||
| const { labels } = await kmeans({ | ||
| pixels: filtered, | ||
| width, | ||
| height, | ||
| num_colors: 16, | ||
| }); | ||
|
|
||
| const { svg } = await findContours({ pixels: filtered, labels, width, height }); | ||
| ``` | ||
|
|
||
|
Prachi-Gupta2808 marked this conversation as resolved.
Resolved
|
||
| > **Note:** `imageToUint8ClampedArray` uses browser APIs (`Image`, `canvas`) and is only available in browser environments. In Node.js, use a library like `sharp` to decode images to a `Uint8ClampedArray` in RGBA format before passing to the WASM functions. | ||
|
Ryan-Millard marked this conversation as resolved.
Resolved
Prachi-Gupta2808 marked this conversation as resolved.
Resolved
|
||
|
|
||
| ## API Reference | ||
|
|
||
| All WASM-backed functions are `async` and return Promises. For full details see the [JavaScript API reference](https://ryan-millard.github.io/Img2Num/info/docs/next/js/). | ||
|
|
||
| ### `imageToUint8ClampedArray(file)` *(browser only)* | ||
|
|
||
| Converts an image `File` object to raw RGBA pixel data. | ||
|
|
||
| | Parameter | Type | Description | | ||
| | :-------- | :----- | :----------------------- | | ||
| | `file` | `File` | A valid image file input | | ||
|
|
||
| **Returns:** `Promise<{ pixels: Uint8ClampedArray, width: number, height: number }>` | ||
|
|
||
| --- | ||
|
|
||
| ### `gaussianBlur({ pixels, width, height, sigma_pixels? })` | ||
|
|
||
| Applies an FFT-based Gaussian blur. | ||
|
|
||
| | Parameter | Type | Default | | ||
| | :------------- | :------------------ | :-------------- | | ||
| | `pixels` | `Uint8ClampedArray` | required | | ||
| | `width` | `number` | required | | ||
| | `height` | `number` | required | | ||
| | `sigma_pixels` | `number` | `width * 0.005` | | ||
|
|
||
| **Returns:** `Promise<Uint8ClampedArray>` | ||
|
|
||
| --- | ||
|
|
||
| ### `bilateralFilter({ pixels, width, height, sigma_spatial?, sigma_range?, color_space? })` | ||
|
|
||
| Applies an edge-preserving bilateral filter. | ||
|
|
||
| | Parameter | Type | Default | | ||
| | :-------------- | :------------------ | :------------------------- | | ||
| | `pixels` | `Uint8ClampedArray` | required | | ||
| | `width` | `number` | required | | ||
| | `height` | `number` | required | | ||
| | `sigma_spatial` | `number` | `3` | | ||
| | `sigma_range` | `number` | `50` | | ||
| | `color_space` | `number` | `0` (CIE LAB; `1` = sRGB) | | ||
|
|
||
| **Returns:** `Promise<Uint8ClampedArray>` | ||
|
|
||
| --- | ||
|
|
||
| ### `blackThreshold({ pixels, width, height, num_colors })` | ||
|
|
||
| Reduces image to N colors biased toward dark output. | ||
|
|
||
| | Parameter | Type | Default | | ||
| | :----------- | :------------------ | :------- | | ||
| | `pixels` | `Uint8ClampedArray` | required | | ||
| | `width` | `number` | required | | ||
| | `height` | `number` | required | | ||
| | `num_colors` | `number` | required | | ||
|
|
||
| **Returns:** `Promise<Uint8ClampedArray>` | ||
|
|
||
| --- | ||
|
|
||
| ### `kmeans({ pixels, width, height, num_colors, max_iter?, color_space? })` | ||
|
|
||
| Clusters image pixels using K-Means. | ||
|
|
||
| | Parameter | Type | Default | | ||
| | :------------ | :------------------ | :------------------------- | | ||
| | `pixels` | `Uint8ClampedArray` | required | | ||
| | `width` | `number` | required | | ||
| | `height` | `number` | required | | ||
| | `num_colors` | `number` | required | | ||
| | `max_iter` | `number` | `100` | | ||
| | `color_space` | `number` | `0` (CIE LAB; `1` = sRGB) | | ||
|
|
||
| **Returns:** `Promise<{ pixels: Uint8ClampedArray, labels: Int32Array }>` | ||
|
|
||
| --- | ||
|
|
||
| ### `findContours({ pixels, labels, width, height, min_area?, min_thickness? })` | ||
|
|
||
| Converts a labeled region map to SVG paths. | ||
|
|
||
| | Parameter | Type | Default | | ||
| | :-------------- | :------------------ | :------- | | ||
| | `pixels` | `Uint8ClampedArray` | required | | ||
| | `labels` | `Int32Array` | required | | ||
| | `width` | `number` | required | | ||
| | `height` | `number` | required | | ||
| | `min_area` | `number` | `100` | | ||
| | `min_thickness` | `number` | `10` | | ||
|
|
||
| **Returns:** `Promise<{ svg: string }>` | ||
|
|
||
| --- | ||
|
|
||
| ### `imageToSvg({ pixels, width, height, ...options? })` | ||
|
|
||
| All-in-one pipeline: bilateral filter → kmeans → findContours. | ||
|
|
||
| | Parameter | Type | Default | | ||
| | :-------------- | :------------------ | :------------------------- | | ||
| | `pixels` | `Uint8ClampedArray` | required | | ||
| | `width` | `number` | required | | ||
| | `height` | `number` | required | | ||
| | `sigma_spatial` | `number` | `3` | | ||
| | `sigma_range` | `number` | `50` | | ||
| | `num_colors` | `number` | `16` | | ||
| | `max_iter` | `number` | `100` | | ||
| | `min_area` | `number` | `100` | | ||
| | `min_thickness` | `number` | `10` | | ||
| | `color_space` | `number` | `0` (CIE LAB; `1` = sRGB) | | ||
|
|
||
| **Returns:** `Promise<{ svg: string }>` | ||
|
|
||
| --- | ||
|
|
||
|
Prachi-Gupta2808 marked this conversation as resolved.
Resolved
|
||
| ## Navigating WebAssembly | ||
|
|
||
| This package ships a `.wasm` binary and a worker file. The library automatically selects the correct worker implementation for your runtime — browser Web Workers or Node.js `worker_threads`. Some bundlers need extra configuration to handle `.wasm` assets correctly: | ||
|
|
||
| - **Vite** — add to `vite.config.js`: | ||
| ```js | ||
| assetsInclude: ["**/*.wasm"] | ||
| ``` | ||
| - **Webpack 5** — enable `asyncWebAssembly: true` in `experiments`. | ||
| - **Other bundlers** — if you run into issues, please [open an issue](https://github.com/Ryan-Millard/Img2Num/issues) so we can document the solution and help others facing the same problem. | ||
|
|
||
| We actively welcome contributions to this section — if you've configured a bundler not listed here, please open a PR to add it to our [documentation](https://ryan-millard.github.io/Img2Num/info/docs/). | ||
|
|
||
| ## Examples | ||
|
|
||
| See the React example app at [`example-apps/react-js`](https://github.com/Ryan-Millard/Img2Num/tree/main/example-apps/react-js) for a full browser integration, or the Node.js console example at [`example-apps/console-js`](https://github.com/Ryan-Millard/Img2Num/tree/main/example-apps/console-js). | ||
|
|
||
| Try the [live demo](https://ryan-millard.github.io/Img2Num/). | ||
|
Prachi-Gupta2808 marked this conversation as resolved.
Resolved
|
||
|
|
||
| ## Build & Test | ||
|
|
||
| The WASM module is compiled from C++ using Emscripten. See the [contributor setup docs](https://ryan-millard.github.io/Img2Num/info/docs/next/contributing/setup-and-dependencies/) for full toolchain setup. | ||
|
|
||
| ```bash | ||
| # Build the WASM (from repo root, inside Docker) | ||
| just build js | ||
| ``` | ||
|
|
||
|
Prachi-Gupta2808 marked this conversation as resolved.
Resolved
|
||
| ```bash | ||
| # Run tests | ||
| npx vitest run | ||
| ``` | ||
|
Prachi-Gupta2808 marked this conversation as resolved.
Resolved
|
||
|
|
||
| ## License | ||
|
|
||
| [MIT](https://github.com/Ryan-Millard/Img2Num/blob/main/LICENSE) © Ryan Millard | ||
|
|
||
| --- | ||
|
|
||
| [Documentation](https://ryan-millard.github.io/Img2Num/info/docs/) · [Changelog](https://github.com/Ryan-Millard/Img2Num/blob/main/packages/js/CHANGELOG.md) · [GitHub](https://github.com/Ryan-Millard/Img2Num) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.