Created
October 25, 2017 12:35
-
-
Save nullpixel/7b4164af85272129b1eecf169b639634 to your computer and use it in GitHub Desktop.
Fill a canvas.place pixel database with information from an image
This file contains 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
const getPixels = require('get-pixels'); | |
const path = require('path'); | |
const mongoose = require("mongoose"); | |
const config = require('../config/config'); | |
const Pixel = require('../models/pixel'); | |
mongoose.connect(config.database).then(() => console.info('Connected to database')); | |
const batch_size = 1000; | |
let toSave = []; | |
getPixels('latest.png', async(err, pixels) => { | |
if (err) throw err; | |
for (let x = 0; x < pixels.shape[0]; x += 1) { | |
for (let y = 0; y < pixels.shape[1]; y += 1) { | |
const r = pixels.get(x, y, 0); | |
const g = pixels.get(x, y, 1); | |
const b = pixels.get(x, y, 2); | |
toSave.append({ | |
xPos: x, | |
yPos: y, | |
editorID: null, | |
lastModified: new Date(), | |
colourR: r, | |
colourG: g, | |
colourB: b | |
}); | |
if (toSave.length === batch_size) { | |
await (Pixel.insertMany(toSave)); | |
toSave = []; | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
made with </> and ❤️ by https://timelapse.canvas.place haha