Skip to content

Instantly share code, notes, and snippets.

@skeletoncrewrip
Last active May 24, 2023 09:18
Show Gist options
  • Save skeletoncrewrip/801ec261061a8bcfefe29459793ecfad to your computer and use it in GitHub Desktop.
Save skeletoncrewrip/801ec261061a8bcfefe29459793ecfad to your computer and use it in GitHub Desktop.
Enlarge the size of PNGs by repeatedly writing a string to metadata
import png from 'png-metadata';
import fs from 'fs';
const desiredSize = 24000;
const folder = './assets/';
const creatorString = 'Skeleton Crew';
const fillerString = 'https://treattoolbox.com ';
const bumpImageSize = async (file) => {
if (file.endsWith('.png')) {
const filePath = folder + file;
let fileSize = (await fs.promises.stat(filePath)).size;
if (fileSize <= desiredSize) {
console.log('bumping ' + filePath);
// load from file
let s = png.readFileSync(filePath);
// split
let list = png.splitChunk(s);
// append
let iend = list.pop(); // remove IEND
const creatorSize = getBytes(creatorString);
let creatorChunk = png.createChunk('aaAa', creatorString);
list.push(creatorChunk);
fileSize += creatorSize;
let fillingString = '';
while (fileSize + getBytes(fillingString) <= desiredSize) {
// const fillingSize = getBytes(fillingString);
fillingString += fillerString;
}
let fillingChunk = png.createChunk('aaAb', fillingString)
list.push(fillingChunk);
list.push(iend);
// join and save
let newpng = png.joinChunk(list);
fs.writeFileSync(filePath, newpng, 'binary');
}
}
}
function getBytes(string) {
return Buffer.byteLength(string, 'utf8')
}
fs.readdirSync(folder).forEach(file => bumpImageSize(file));
@skeletoncrewrip
Copy link
Author

First run:
npm i --global png-metadata
then:
node png_increase.mjs
and there should be a directory in the same location as the script that contains all your images and metadata named assets

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