GithubHelp home page GithubHelp logo

foo123 / img2svg Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 1.34 MB

Vectorize Image Data to SVG using potrace algorithm with color

Home Page: https://foo123.github.io/examples/img2svg/

License: GNU General Public License v3.0

JavaScript 99.33% Batchfile 0.36% HTML 0.30%
potrace-algorithm image-to-svg

img2svg's Introduction

img2svg

Vectorize Image Data to SVG using potrace algorithm v.1.16 with color

version 1.0.0 (17 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, {mode:"color",depth:5,turdsize:0,minpathsegments:2}));
};
img.src = __dirname + '/test.jpeg';

demo in browser:

function el(html)
{
    const container = document.createElement('div');
    container.innerHTML = html;
    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, {mode:"color",depth:5,turdsize:0,minpathsegments:2});
    document.body.appendChild(img);
    document.body.appendChild(el(svg));
};
img.src = './test.jpeg';

Result:

img2svg demo

Options:

  • mode: "hue" | "gray" (for grayscale images) | "color" (default)
  • depth: depth of color quantization (default 2)
  • depthR,depthG,depthB,depthA: depth per separate image channel (default depth)

POTRACE Options:

  • connectedcomponents: (separate disconnected paths) true (default)
  • minpathsegments: (minimum number of path points) 2 (default)
  • turnpolicy: "left" | "right" | "black" | "white" | "majority" | "minority" (default)
  • turdsize: (minimum area to trace) 2 (default)
  • alphamax: (balance between more curves vs more corners) 1 (default)
  • optcurve: (generate optimum curves) true (default)
  • opttolerance: (tolerance for optimum curves) 0.2 (default)

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

img2svg's People

Contributors

foo123 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

krakn-dev

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.