title: Cannot convert an image to another format using canvas
body: <h2>Problem</h2>
  <p>I'm trying to convert an image to another format using canvas, and then save the image to disk (using Node.js), I'm not sure why it's not working.</p>
  <p>I don't really need the canvas itself, I'm just using it to convert an image. Should I append the canvas into an HTML element to make it work?</p>
  <h2>Code</h2>
  <pre><code>convertImage () {
      var image = new Image(); 
      image.src = 'C:/test/test.png';
      
      // Converts image to canvas
      var canvas = document.createElement(&quot;canvas&quot;);
      canvas.width = image.width;
      canvas.height = image.height;
      // Do I even need to draw it?
      canvas.getContext(&quot;2d&quot;).drawImage(image, 0, 0);
  
      // Converts canvas to a data url
      var imageOut = new Image();
      let base64data = canvas.toDataURL(&quot;image/png&quot;);
      imageOut.src = base64data;
  
      var data = base64data.replace(/^data:image\/\w+;base64,/, &quot;&quot;);
      var buffer = new Buffer(data, 'base64');
      fs.writeFile('C:/test/converted.jpg', buffer);
  }
  </code></pre>