Skip to content

Instantly share code, notes, and snippets.

Last active May 24, 2023 09:18
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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 = ' ';
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);
fileSize += creatorSize;
let fillingString = '';
while (fileSize + getBytes(fillingString) <= desiredSize) {
// const fillingSize = getBytes(fillingString);
fillingString += fillerString;
let fillingChunk = png.createChunk('aaAb', fillingString)
// 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));
Copy link

First run:
npm i --global png-metadata
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