Created
September 9, 2022 13:53
-
-
Save Linux249/d9d1444c3ffca25358a47c13a7164ae8 to your computer and use it in GitHub Desktop.
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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use to create tousend of random .png files