Skip to content

Instantly share code, notes, and snippets.

@FOLLGAD
Last active January 21, 2021 18:39
Show Gist options
  • Select an option

  • Save FOLLGAD/7e237e2eca983191be61535e4266bd77 to your computer and use it in GitHub Desktop.

Select an option

Save FOLLGAD/7e237e2eca983191be61535e4266bd77 to your computer and use it in GitHub Desktop.
Use node to make a png into an array of 1s and 0s (black and white)
let fs = require('fs'),
PNG = require('pngjs').PNG;
fs.createReadStream('./dino_design20by20.png')
.pipe(new PNG({
filterType: 4
}))
.on('parsed', function () {
let arr = []
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
let idx = (this.width * y + x) << 2
let isFilled = this.data[idx] < 128
arr.push(isFilled ? 1 : 0)
}
}
fs.writeFileSync('data.json', JSON.stringify(arr), () => { })
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment