Enlarge the size of PNGs by repeatedly writing a string to metadata
This file contains 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
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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