Skip to content
/ img2svg Public

Vectorize Image Data to SVG using POTRACE. Based on multilabel-potrace by Hugo Raguet, which is based on potrace by Peter Selinger.

License

Notifications You must be signed in to change notification settings

foo123/img2svg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

img2svg

Vectorize Image Data to SVG using POTRACE Based on multilabel-potrace by Hugo Raguet, which is based on potrace by Peter Selinger

version 2.0.0 (25 kB minified)

demo in nodejs with CanvasLite:

const CanvasLite = require('./CanvasLite.js');
const img2svg = require('../build/img2svg.js');
const img = new CanvasLite.Image(), canvas = new CanvasLite();
img.onload = () => {
    canvas.width = img.width;
    canvas.height = img.height;
    canvas.getContext('2d').drawImage(img, 0, 0);
    const imgData = canvas.getContext('2d').getImageData(0, 0, img.width, img.height);
    console.log(img2svg(imgData, {depth:16}));
};
img.src = __dirname + '/test.jpeg';

demo in browser:

function el(html)
{
    const container = document.createElement('div');
    container.innerHTML = html.trim();
    return container.firstChild;
}
const img = new Image(), canvas = document.createElement('canvas');
img.onload = () => {
    canvas.width = img.width;
    canvas.height = img.height;
    canvas.getContext('2d').drawImage(img, 0, 0);
    const imgData = canvas.getContext('2d').getImageData(0, 0, img.width, img.height);
    const svg = img2svg(imgData, {depth:16});
    document.body.appendChild(img);
    document.body.appendChild(el(svg));
};
img.src = './test.jpeg';

Result:

img2svg demo

Options:

  • depth: depth of color quantization for all channels (default 16)
  • depthR,depthG,depthB: depth of color quantization per separate image channel (default depth)
  • transparency: level of ALPHA channel, from 0 to 100, under which area is considered transparent and is ignored (default 50)
  • layered: separate into layers of overlapping connected components instead of isolated connected components (default false)
  • outline: line width to generate outline of image only (default 0)

POTRACE Options:

  • minpathsegments: ignore areas with less number of segments than this (default 0)
  • turdsize: ignore areas with size smaller or equal to this (default 0)
  • linetolerance: straight line tolerance (default 0.5)
  • alphamax: balance between more smooth curves vs more lines and corners (default 1.0)
  • opttolerance: tolerance for generating optimum curves if > 0.0 (default 0.2)

see also:

  • CanvasLite an html canvas implementation in pure JavaScript
  • Rasterizer stroke and fill lines, rectangles, curves and paths, without canvaσ
  • Gradient create linear, radial, conic and elliptic gradients and image patterns without canvas
  • Geometrize Computational Geometry and Rendering Library for JavaScript
  • Plot.js simple and small library which can plot graphs of functions and various simple charts and can render to Canvas, SVG and plain HTML
  • MOD3 3D Modifier Library in JavaScript
  • HAAR.js image feature detection based on Haar Cascades in JavaScript (Viola-Jones-Lienhart et al Algorithm)
  • HAARPHP image feature detection based on Haar Cascades in PHP (Viola-Jones-Lienhart et al Algorithm)
  • FILTER.js video and image processing and computer vision Library in pure JavaScript (browser and node)
  • css-color simple class to parse and manipulate colors in various formats
  • img2svg vectorize image data to svg
  • svg2json parse svg to json

About

Vectorize Image Data to SVG using POTRACE. Based on multilabel-potrace by Hugo Raguet, which is based on potrace by Peter Selinger.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages