Skip to content

Instantly share code, notes, and snippets.

@nullpixel
Created October 25, 2017 12:35
Show Gist options
  • Save nullpixel/7b4164af85272129b1eecf169b639634 to your computer and use it in GitHub Desktop.
Save nullpixel/7b4164af85272129b1eecf169b639634 to your computer and use it in GitHub Desktop.
Fill a canvas.place pixel database with information from an image
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 = [];
}
}
}
});
@deadbaed
Copy link

deadbaed commented May 9, 2018

made with </> and ❤️ by https://timelapse.canvas.place haha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment