Posted By

chrisaiv on 01/22/15


Tagged

javascript js nodejs


Versions (?)

NodeJS: Turn image into Vector art silhouette


 / Published in: JavaScript
 

URL: http://brewformulas.org/Potrace

I was recently asked to turn a bitmap image (.bmp) into a vector graphic and I wanted to do it using Javascript. NodeJS did the trick with a little help from Node Package Manager and Brew Package Manager. The first step was to install potrace then install a few other node packages like Async.

NOTE! USE BMP, not JPG or PNG. Potrace prefers that format.

  1. var sys = require('sys')
  2. , exec = require('child_process').exec
  3. , async = require('async')
  4. , util = require('util')
  5.  
  6. //!!!Notice that I am removing the file extension
  7. var images = [
  8. "file1",
  9. "file2",
  10. "file3"
  11. ]
  12.  
  13. async.mapSeries(
  14. images,
  15. function(image, callback){
  16. //Manual
  17. //http://potrace.sourceforge.net/potrace.1.html
  18. var params = {
  19. input: "bmp/" + image + ".bmp",
  20. output: "-o " + "eps/" + image + ".eps",
  21. resolution: "--resolution 150",
  22. opaque: "--opaque",
  23. alphamax: "--alphamax 0.2",
  24. curve: "--opttolerance 0.5",
  25. progress: "--progress"
  26. }
  27. //Simple Example
  28. //exec("potrace " + composer + ".bmp -o " + image + ".eps " , callback);
  29. exec(
  30. util.format( "potrace " + "%s %s %s %s %s %s %s",
  31. params.input,
  32. params.output,
  33. params.resolution,
  34. params.opaque,
  35. params.alphamax,
  36. params.curve,
  37. params.progress
  38. )
  39. , callback)
  40. },
  41. function(err, results) {
  42. if(err) console.error(err.message)
  43. else console.log(results)
  44. }
  45. )

Report this snippet  

You need to login to post a comment.