Skip to content

Instantly share code, notes, and snippets.

@baktiaditya
Created November 17, 2021 16:39
Show Gist options
  • Save baktiaditya/f91095fb0bdf4e1eab842f4f2af5fbe4 to your computer and use it in GitHub Desktop.
Save baktiaditya/f91095fb0bdf4e1eab842f4f2af5fbe4 to your computer and use it in GitHub Desktop.
import { ImageData, getNounData, getRandomNounSeed, NounSeed } from '@nouns/assets';
import { buildSVG } from '@nouns/sdk';
import isEqual from 'lodash/isEqual';
import fs from 'fs';
const svgToImg = require('svg-to-img');
const keys = Object.keys as <T>(o: T) => Extract<keyof T, string>[];
const totalImage = 10_000;
const directory = 'generated-head-dao';
(async () => {
if (fs.existsSync(directory)) {
fs.rmdirSync(directory, { recursive: true });
}
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
const seeds: NounSeed[] = [];
while (seeds.length < totalImage) {
const seed = getRandomNounSeed();
const hasDuplicates = seeds.some(currentObject => {
return isEqual(currentObject, seed);
});
if (!hasDuplicates) {
const { parts, background } = getNounData(seed);
const svg = buildSVG(parts, ImageData.palette, background);
let filename = '';
keys(seed).forEach((key, index) => {
const isLast = index === keys(seed).length - 1;
filename += `${key}-${seed[key]}${!isLast ? '-' : ''}`;
});
const image = await svgToImg.from(svg).toPng({ width: 630 });
fs.writeFile(`${directory}/${filename}.png`, image, err => {
if (err) throw err;
seeds.push(seed);
});
} else {
console.log('duplicate found');
}
}
console.log(`Total image generated:`, seeds.length);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment