oslllo-potrace 5.0.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +115 -81
- package/package.json +10 -4
package/README.md
CHANGED
|
@@ -1,127 +1,161 @@
|
|
|
1
1
|
# Potrace
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[](https://www.npmjs.com/package/oslllo-potrace)
|
|
3
|
+
Trace raster images into SVG outlines with a JavaScript port of Potrace JS for Node.js.
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
[](https://github.com/oslllo/potrace/actions/workflows/ci.test.yml)
|
|
6
|
+
[](https://www.npmjs.com/package/oslllo-potrace)
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
| Original image | Traced output |
|
|
9
|
+
| --- | --- |
|
|
10
|
+
|  |  |
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|---------------------------|------------------------------|
|
|
12
|
-
|  |  |
|
|
12
|
+
The project aims to match the output of the original Potrace JS port. It reads an image with Jimp, converts its pixels to a binary bitmap, finds outlines, and generates SVG path data.
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
## Contents
|
|
15
15
|
|
|
16
|
-
[
|
|
16
|
+
- [Installation](#installation)
|
|
17
|
+
- [Quick start](#quick-start)
|
|
18
|
+
- [Input and output](#input-and-output)
|
|
19
|
+
- [Tracing options](#tracing-options)
|
|
20
|
+
- [Rendering a preview](#rendering-a-preview)
|
|
21
|
+
- [Development](#development)
|
|
22
|
+
- [Changelog and license](#changelog-and-license)
|
|
17
23
|
|
|
18
|
-
|
|
24
|
+
## Installation
|
|
19
25
|
|
|
20
|
-
|
|
26
|
+
**Requires Node.js 22.12 or later.** The package uses Jimp 1.7 with patched image detection and validation dependencies. CI tests Node.js 22 and 24 on Linux, macOS, and Windows.
|
|
21
27
|
|
|
22
|
-
|
|
28
|
+
```sh
|
|
29
|
+
npm install oslllo-potrace
|
|
30
|
+
# Or:
|
|
31
|
+
yarn add oslllo-potrace
|
|
32
|
+
```
|
|
23
33
|
|
|
24
|
-
|
|
34
|
+
This README describes the current source on `master`. Check the release's requirements when using an older package version.
|
|
25
35
|
|
|
26
|
-
|
|
36
|
+
## Quick start
|
|
27
37
|
|
|
28
|
-
|
|
38
|
+
```js
|
|
39
|
+
const fs = require("node:fs/promises");
|
|
40
|
+
const Potrace = require("oslllo-potrace");
|
|
29
41
|
|
|
30
|
-
|
|
42
|
+
async function main() {
|
|
43
|
+
const svg = await Potrace("image.png").trace();
|
|
44
|
+
await fs.writeFile("traced.svg", svg);
|
|
45
|
+
console.log("Saved traced.svg");
|
|
46
|
+
}
|
|
31
47
|
|
|
32
|
-
|
|
33
|
-
npm install oslllo-potrace
|
|
48
|
+
main().catch(console.error);
|
|
34
49
|
```
|
|
35
50
|
|
|
36
|
-
|
|
51
|
+
The following examples use the same `Potrace` import. Examples containing `await` belong inside an async function.
|
|
52
|
+
|
|
53
|
+
## Input and output
|
|
37
54
|
|
|
38
|
-
|
|
55
|
+
`Potrace(image, options?)` creates an instance, with or without `new`.
|
|
39
56
|
|
|
40
|
-
|
|
57
|
+
| Parameter | Meaning |
|
|
58
|
+
| --- | --- |
|
|
59
|
+
| `image` | Image file path as a string, or a Buffer containing encoded image bytes |
|
|
60
|
+
| `options` | Optional tracing settings described below |
|
|
41
61
|
|
|
42
|
-
|
|
62
|
+
Input formats are those decoded by the installed Jimp version, including PNG, JPEG, BMP, TIFF, and GIF. Pass encoded image data, rather than raw RGBA pixels or SVG markup.
|
|
43
63
|
|
|
44
|
-
|
|
64
|
+
`.trace()` returns a Promise resolving to an SVG string. It does not write files or accept a callback. Invalid input and decoding errors reject the Promise.
|
|
45
65
|
|
|
46
66
|
```js
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
const Svg2 = require("oslllo-svg2");
|
|
67
|
+
const image = await require("node:fs/promises").readFile("image.png");
|
|
68
|
+
const svg = await Potrace(image).trace();
|
|
69
|
+
```
|
|
51
70
|
|
|
52
|
-
|
|
53
|
-
var source = path.resolve("example/tree.jpg");
|
|
54
|
-
var destination = path.resolve("example/tree.svg");
|
|
55
|
-
var traced = await Potrace(source).trace();
|
|
56
|
-
fs.writeFileSync(destination, traced);
|
|
57
|
-
await Svg2(traced).png().extend(10).toFile("example/tree.png");
|
|
58
|
-
}
|
|
71
|
+
Create a new instance for each trace. Tracing stores the decoded image and accumulated paths on the instance.
|
|
59
72
|
|
|
60
|
-
|
|
61
|
-
console.log("done");
|
|
62
|
-
}).catch((err) => {
|
|
63
|
-
throw err;
|
|
64
|
-
});
|
|
65
|
-
```
|
|
73
|
+
The default SVG contains one black filled path, which may contain multiple subpaths, with `stroke="none"` and `fill-rule="evenodd"`. Width and height are based on the input bitmap and `svgSize`.
|
|
66
74
|
|
|
67
|
-
|
|
75
|
+
Tracing is monochrome: pixels with weighted RGB luminance below `128` become foreground; other pixels become background. The threshold is fixed, and alpha is not used in this calculation. Composite transparent artwork onto a suitable background before tracing if needed. Original colors and image metadata are not preserved.
|
|
68
76
|
|
|
69
|
-
|
|
77
|
+
## Tracing options
|
|
70
78
|
|
|
71
79
|
```js
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
80
|
+
const svg = await Potrace("image.png", {
|
|
81
|
+
turnpolicy: Potrace.TURNPOLICY_MINORITY,
|
|
82
|
+
turdsize: 2,
|
|
83
|
+
optcurve: true,
|
|
84
|
+
alphamax: 1,
|
|
85
|
+
opttolerance: 0.2,
|
|
86
|
+
svgSize: 1,
|
|
87
|
+
}).trace();
|
|
88
|
+
```
|
|
76
89
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
90
|
+
| Option | Default | Meaning |
|
|
91
|
+
| --- | --- | --- |
|
|
92
|
+
| `turnpolicy` | `Potrace.TURNPOLICY_MINORITY` | Resolve ambiguities when decomposing bitmap outlines |
|
|
93
|
+
| `turdsize` | `2` | Suppress small speckles by area in bitmap pixels |
|
|
94
|
+
| `optcurve` | `true` | Enable curve optimization |
|
|
95
|
+
| `alphamax` | `1` | Corner threshold controlling the balance between corners and curves |
|
|
96
|
+
| `opttolerance` | `0.2` | Curve optimization tolerance |
|
|
97
|
+
| `svgSize` | `1` | Scale output dimensions and path coordinates by this factor |
|
|
98
|
+
| `opt_type` | `undefined` | Default filled output; set to `"curve"` for black stroked outlines with no fill |
|
|
99
|
+
|
|
100
|
+
Available turn policies:
|
|
101
|
+
|
|
102
|
+
| Constant | Value |
|
|
103
|
+
| --- | --- |
|
|
104
|
+
| `Potrace.TURNPOLICY_BLACK` | `"black"` |
|
|
105
|
+
| `Potrace.TURNPOLICY_WHITE` | `"white"` |
|
|
106
|
+
| `Potrace.TURNPOLICY_LEFT` | `"left"` |
|
|
107
|
+
| `Potrace.TURNPOLICY_RIGHT` | `"right"` |
|
|
108
|
+
| `Potrace.TURNPOLICY_MINORITY` | `"minority"` |
|
|
109
|
+
| `Potrace.TURNPOLICY_MAJORITY` | `"majority"` |
|
|
110
|
+
|
|
111
|
+
Defaults are also exposed as `Potrace.TURDSIZE`, `Potrace.OPTCURVE`, `Potrace.ALPHAMAX`, and `Potrace.OPTTOLERANCE`. Option values are merged with defaults without validation; use values of the documented types.
|
|
112
|
+
|
|
113
|
+
```js
|
|
114
|
+
// A 100 × 50 bitmap produces a 200 × 100 SVG.
|
|
115
|
+
const larger = await Potrace("image.png", { svgSize: 2 }).trace();
|
|
116
|
+
|
|
117
|
+
// Inspect the traced curves as stroked outlines.
|
|
118
|
+
const outlines = await Potrace("image.png", { opt_type: "curve" }).trace();
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
`svgSize` scales the traced result; it does not increase source image detail.
|
|
85
122
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
123
|
+
## Rendering a preview
|
|
124
|
+
|
|
125
|
+
Potrace returns SVG markup. To turn it into a PNG preview, install SVG2 separately:
|
|
126
|
+
|
|
127
|
+
```sh
|
|
128
|
+
npm install oslllo-svg2
|
|
91
129
|
```
|
|
92
130
|
|
|
93
|
-
|
|
131
|
+
The current SVG2 source requires Node.js 22+. Use a runtime compatible with the SVG2 version you install.
|
|
94
132
|
|
|
95
|
-
|
|
133
|
+
```js
|
|
134
|
+
const Svg2 = require("oslllo-svg2");
|
|
96
135
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
- `alphamax`: ([**Number**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)) corner threshold parameter **(default: 1)**.
|
|
103
|
-
- `opttolerance`: ([**Number**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)) curve optimization tolerance **(default: 0.2)**.
|
|
104
|
-
- `svgSize`: ([**Number**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)) set svg output size. ***NOTE:*** `The resulting image size` <b>=</b> `the original image size` <b>*</b> `size` **(default: 1)**.
|
|
105
|
-
- `opt_type`: ([**String**](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)) output svg type, available options are [`curve`]
|
|
136
|
+
const svg = await Potrace("image.png").trace();
|
|
137
|
+
await Svg2(svg).png().extend(10).toFile("preview.png");
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
SVG2 is optional and is not a runtime dependency of Potrace. SVG Fixer uses the two libraries together: SVG2 renders an input SVG to a bitmap, and Potrace traces that bitmap back to a filled SVG path.
|
|
106
141
|
|
|
107
|
-
##
|
|
142
|
+
## Development
|
|
108
143
|
|
|
109
|
-
|
|
144
|
+
CI runs on pull requests targeting `master`, pushes to `master`, and manual dispatch. Tests cover Node.js 22 and 24 on Linux, macOS, and Windows, plus Node.js 22.12.0 on Linux to verify the minimum supported version.
|
|
110
145
|
|
|
111
|
-
|
|
146
|
+
The dependency overrides select patched `glob` and `minimatch` versions for the test tooling. Keep these overrides until upstream dependencies adopt the patched versions.
|
|
112
147
|
|
|
113
|
-
```
|
|
148
|
+
```sh
|
|
149
|
+
npm ci
|
|
114
150
|
npm test
|
|
115
151
|
```
|
|
116
152
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
[Potrace JS Port](https://github.com/kilobtye/potrace) by @kilobtye.
|
|
153
|
+
Tests cover tracing from paths and Buffers, options, invalid input, example images, and exact SVG output comparisons against fixtures. Run the commands from the repository root.
|
|
120
154
|
|
|
121
|
-
|
|
155
|
+
`npm run example` traces the bundled tree image and renders previews using the development dependency `svg2png-wasm-node-10`. It writes output files under `example/`. The declared `npm run debug` command currently points to a missing `test/debug/debug.js` file and is not usable in this checkout.
|
|
122
156
|
|
|
123
|
-
|
|
157
|
+
## Changelog and license
|
|
124
158
|
|
|
125
|
-
|
|
159
|
+
See [CHANGELOG.md](CHANGELOG.md) for release history and [LICENSE](LICENSE) for the GPL-2.0 license.
|
|
126
160
|
|
|
127
|
-
|
|
161
|
+
Based on [Potrace JS](https://github.com/kilobtye/potrace) by kilobtye and the original Potrace algorithm by Peter Selinger. The project was motivated by output differences discussed in [node-potrace issue #7](https://github.com/tooolbox/node-potrace/issues/7).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oslllo-potrace",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "A \"1:1 output\" JavaScript port of Potrace JS for NodeJS.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"files": [
|
|
@@ -37,16 +37,22 @@
|
|
|
37
37
|
"bugs": {
|
|
38
38
|
"url": "https://github.com/oslllo/potrace/issues"
|
|
39
39
|
},
|
|
40
|
-
"homepage": "https://github.com/oslllo/potrace#readme",
|
|
41
40
|
"dependencies": {
|
|
42
|
-
"jimp": "^1.
|
|
41
|
+
"jimp": "^1.7.0"
|
|
43
42
|
},
|
|
44
43
|
"devDependencies": {
|
|
45
44
|
"chai": "^4.5.0",
|
|
46
45
|
"chai-as-promised": "^7.1.2",
|
|
47
46
|
"fast-glob": "^3.3.3",
|
|
48
47
|
"fs-extra": "^10.1.0",
|
|
49
|
-
"mocha": "^
|
|
48
|
+
"mocha": "^12.0.3",
|
|
50
49
|
"svg2png-wasm-node-10": "^1.3.1"
|
|
50
|
+
},
|
|
51
|
+
"overrides": {
|
|
52
|
+
"glob": "^14.0.7",
|
|
53
|
+
"minimatch": "^11.0.10"
|
|
54
|
+
},
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=22.12.0"
|
|
51
57
|
}
|
|
52
58
|
}
|