oslllo-potrace
A "1:1 output" JavaScript port of Potrace JS for NodeJS.
Last updated 20 days ago by ghustavh97 .
GPL-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install oslllo-potrace 
SYNC missed versions from official npm registry.

Potrace

Travis (.org) npm

A "1:1 output" JavaScript port of Potrace JS for NodeJS.


Original image Potrace output
Original Image Potrace Output

Online Demo


Why did I create this package ❓

The issue


Installation

npm install oslllo-potrace

Usage

Taken from Example.js

Example Setup

// Require packages
const potrace = require("oslllo-potrace");
const fs = require("fs-extra");
const path = require("path");
const sharp = require("sharp");

// Setup paths
const SOURCE_IMAGE_PATH = path.resolve("example/tree.jpg");
const TRACED_SVG_PATH = path.resolve("example/tree.svg");

// Function to help us save our svg to pngs on disk
async function saveAsPngToDisk(TRACED_SVG_PATH) {
    await sharp(TRACED_SVG_PATH)
        .flatten({ background: "#fff" })
        .png()
        .toFile(path.resolve("example/tree.png"));
}

Using potrace.trace()

async function traceExample() {
    // Trace svg using potrace.trace()
    var tracedSvg = await potrace.trace(SOURCE_IMAGE_PATH);
    // Save svg to disk with .svg extension
    fs.writeFileSync(TRACED_SVG_PATH, tracedSvg);
    // Add white background to svg and save to disk with .png extension
    await saveAsPngToDisk(TRACED_SVG_PATH);
}

Using new potrace.Potrace()

async function potracePotraceExample() {
    // Create new potrace.Potrace() instance
    var trace = new potrace.Potrace();
    // Load image we want to trace into the instance
    await trace.loadImage(SOURCE_IMAGE_PATH);
    // Process the image
    await trace.process();
    // Retrieve our traced svg
    tracedSvg = trace.getSVG();
    // Save svg to disk with .svg extension
    fs.writeFileSync(TRACED_SVG_PATH, tracedSvg);
    // Add white background to svg and save to disk with .png extension
    await saveAsPngToDisk(TRACED_SVG_PATH);
}

Wrapper API (potrace = require("oslllo-potrace"))

  • potrace.trace(file, options): load image from path or Buffer API, process it then return its traced SVG data.

Potrace Class API (potrace = new potrace.Potrace())

  • potrace.loadImage(file, options): load image from path or Buffer API.
  • potrace.setParameter({para1: value, ...}): set parameters.
  • potrace.process(): wait for the image be loaded, then run potrace algorithm. on image.
  • potrace.getSVG(size, opt_type): return a string of generated SVG image. NOTE: The resulting image size = the original image size * size. Optional parameter opt_type can be "curve".

Parameters

  • turnpolicy: how to resolve ambiguities in path decomposition. TURNPOLICY_BLACK, TURNPOLICY_WHITE, TURNPOLICY_LEFT, TURNPOLICY_RIGHT, TURNPOLICY_MINORITY, TURNPOLICY_MAJORITY (default: TURNPOLICY_MINORITY).
  • turdsize: suppress speckles of up to this size (default: 2).
  • optcurve: turn on/off curve optimization (default: true).
  • alphamax: corner threshold parameter (default: 1).
  • opttolerance: curve optimization tolerance (default: 0.2).
  • svgSize: set default svg size for potrace.Potrace.getSVG() (default: 1).

Changelog

Please see CHANGELOG for more information what has changed recently.

Test

npm test

Credits

Original Potrace by Peter Selinger.

Potrace JS Port by @kilobtye.

Another Potrace Port For Node.js maintained by @tooolbox.

License

The GNU General Public License v2.0 (GPL-2.0). Please see License File for more information.

Current Tags

  • 0.0.9                                ...           latest (20 days ago)

9 Versions

  • 0.0.9                                ...           20 days ago
  • 0.0.8                                ...           21 days ago
  • 0.0.7                                ...           24 days ago
  • 0.0.6                                ...           a month ago
  • 0.0.5                                ...           a month ago
  • 0.0.4                                ...           a month ago
  • 0.0.3                                ...           a month ago
  • 0.0.2                                ...           a month ago
  • 0.0.1                                ...           a month ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 57
Dependencies (5)
Dev Dependencies (2)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |