Advertisement
dragonfree97

Untitled

Apr 25th, 2020
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const {
  2.     createCanvas,
  3.     loadImage
  4. } = require('canvas')
  5. const fs = require('fs');
  6. const bigInt = require("big-integer");
  7.  
  8. var image_data = bigInt(process.argv[2]).toString(2);
  9. var image_size = Math.ceil(Math.pow(image_data.length, 0.5));
  10. console.log("Image data (len "+image_data.length+"): "+image_data);
  11. console.log("Image size: "+image_size);
  12. const canvas = createCanvas(image_size, image_size);
  13. const ctx = canvas.getContext('2d');
  14.  
  15. for (var i = 0; i < image_data.length; i++) {
  16.     var x = i%image_size;
  17.     var y = Math.floor(i/image_size);
  18.     if (image_data[i] == "1") {
  19.         ctx.fillStyle = "#000000";
  20.         console.log("pixel ("+x+","+y+") is black");
  21.     } else {
  22.         ctx.fillStyle = "#FFFFFF";
  23.         console.log("pixel ("+x+","+y+") is white");
  24.     }
  25.     ctx.fillRect( x, y, 1, 1 );
  26. }
  27. var d = new Date();
  28. var fn = "output/test_"+(d.getTime())+".png";
  29. var out = fs.createWriteStream(fn)
  30. const stream = canvas.createPNGStream({
  31.     quality: 0.95,
  32.     chromaSubsampling: false
  33. })
  34. stream.pipe(out)
  35. out.on('finish', () => {
  36.     return console.log("done");
  37. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement