HashLips / hashlips_art_engine

HashLips Art Engine is a tool used to create multiple different instances of artworks based on provided layers.
MIT License
7.17k stars 4.3k forks source link

Suggestion: Add max compression for PNG #249

Open borisblizzard opened 2 years ago

borisblizzard commented 2 years ago

Since some blockchains charge more for minting larger images (larger as in more bytes, not larger dimensions), I suggest that PNG ZLIB compression level is set to the maximum value of 9 by default. This produces smaller PNG files, but the compression is still lossless and won't affect the image quality in any way.

This can be achieved by changing this line in main.js (line 111 at the time of writing):

fs.writeFileSync( `${buildDir}/images/${_editionCount}.png`, canvas.toBuffer("image/png") );

To this:

fs.writeFileSync( `${buildDir}/images/${_editionCount}.png`, canvas.toBuffer("image/png", {compressionLevel: 9}) );

cdric commented 2 years ago

That is really cool. Is this as good as running optipng? I usually do run this command prior to uploading my asset and I so save usually 25% without affecting quality.

Btw, I've tried your change and I could not see any difference in terms of file size...

Though after running optipng on my build/images folder I got from 704K to 332K (or a 53% reduction)

borisblizzard commented 2 years ago

That is really cool. Is this as good as running optipng? I usually do run this command prior to uploading my asset and I so save usually 25% without affecting quality.

Btw, I've tried your change and I could not see any difference in terms of file size...

Though after running optipng on my build/images folder I got from 704K to 332K (or a 53% reduction)

Yup, this is like using optipng afterwards. The compression level is defined by the zlib standard and PNG uses zlib compression. All optipng really does is decompress the PNG and compress it again at level 9.