Skip to content

Instantly share code, notes, and snippets.

@Linux249
Created September 9, 2022 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Linux249/d9d1444c3ffca25358a47c13a7164ae8 to your computer and use it in GitHub Desktop.
Save Linux249/d9d1444c3ffca25358a47c13a7164ae8 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const Jimp = require("jimp");
const rootDir = process.cwd();
const outDir = `${rootDir}/img/`;
function createRandomPNG(name, width = 4, height = 4) {
return new Jimp(width, height, function (err, image) {
if (err) throw err;
for (let px = 0; px < width * height; px += 1) {
const color = Jimp.rgbaToInt(
Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256),
255
);
image.setPixelColor(color, px % width, Math.floor(px / width));
}
image.write(outDir + `${name}.png`, (err) => {
if (err) throw err;
});
});
}
/**
* @param count number of images to create
* @param width width of each image
* @param height height of each image
*/
function createManyPNGs(count = 2, width = 400, height = 400) {
// create new folder each start
if (fs.existsSync(outDir)) {
fs.rmdirSync(outDir, { recursive: true });
}
fs.mkdirSync(outDir);
for (let i = 0; i < count; i++) {
createRandomPNG(`${i}`, width, height);
}
}
createManyPNGs(10, 10000, 10000);
@Linux249
Copy link
Author

Linux249 commented Sep 9, 2022

use to create tousend of random .png files

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