Last active
January 21, 2021 18:39
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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