Skip to content

Instantly share code, notes, and snippets.

@baringji
Created December 11, 2021 14:05
Show Gist options
  • Save baringji/5a5a0fc90a6629e67047b1d5c029e923 to your computer and use it in GitHub Desktop.
Save baringji/5a5a0fc90a6629e67047b1d5c029e923 to your computer and use it in GitHub Desktop.
HashLips Art Engine ReEdition
const fs = require("fs");
const path = require("path");
const { createCanvas, loadImage } = require("canvas");
const basePath = process.cwd();
const buildDir = `${basePath}/build/json`;
const inputDir = `${basePath}/build/images`;
const metaDir = `${basePath}/build/meta`;
const editionDir = `${basePath}/build/editions`;
const {
format,
namePrefix,
description,
baseUri,
} = require(`${basePath}/src/config.js`);
const console = require("console");
const canvas = createCanvas(format.width, format.height);
const ctx = canvas.getContext("2d");
const metadataList = [];
const buildSetup = () => {
if (fs.existsSync(metaDir)) {
fs.rmdirSync(metaDir, { recursive: true });
}
if (fs.existsSync(editionDir)) {
fs.rmdirSync(editionDir, { recursive: true });
}
fs.mkdirSync(metaDir);
fs.mkdirSync(editionDir);
};
const getImages = (_dir) => {
try {
return fs
.readdirSync(_dir)
.filter((item) => {
let extension = path.extname(`${_dir}${item}`);
if (extension == ".png" || extension == ".jpg") {
return item;
}
})
.map((i) => {
return {
filename: i,
path: `${_dir}/${i}`,
edition: i,
};
});
} catch {
return null;
}
};
const loadImgData = async (_imgObject) => {
return new Promise(async (resolve) => {
const image = await loadImage(`${_imgObject.path}`);
resolve({ imgObject: _imgObject, loadedImage: image });
});
};
const draw = (_loadedImageObject, i) => {
fs.copyFile(_loadedImageObject.imgObject.path, `${editionDir}/${i}.png`, (err) => {
if (err) throw err;
console.log(`Copying image ${_loadedImageObject.imgObject.path} => ${editionDir}/${i}.png`);
});
};
const saveMetadata = (_loadedImageObject, i) => {
let shortName = _loadedImageObject.imgObject.filename.replace(/\.[^/.]+$/, "");
let rawMeta = fs.readFileSync(`${buildDir}/${shortName}.json`);
let metaData = JSON.parse(rawMeta);
metaData.name = `${namePrefix} #${i}`;
metaData.image = `${baseUri}/${i}.png`;
metaData.edition = Number(i);
fs.writeFileSync(`${metaDir}/${i}.json`, JSON.stringify(metaData, null, 2));
console.log(`Creating metadata ${buildDir}/${shortName}.json => ${metaDir}/${i}.json`);
metadataList.push(metaData);
};
const writeMetaData = (_data) => {
fs.writeFileSync(`${metaDir}/_metadata.json`, _data);
};
const startCreating = async () => {
const images = getImages(inputDir);
if (images == null) {
console.log("Please generate collection first.");
return;
}
let loadedImageObjects = [];
images.forEach((imgObject) => {
loadedImageObjects.push(loadImgData(imgObject));
});
let i = 1;
await Promise.all(loadedImageObjects).then((loadedImageObjectArray) => {
loadedImageObjectArray.forEach((loadedImageObject) => {
draw(loadedImageObject, i);
saveMetadata(loadedImageObject, i);
i++;
});
});
writeMetaData(JSON.stringify(metadataList, null, 2));
};
buildSetup();
startCreating();
@cantonyuu
Copy link

finally works, thanks

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