Open AmjadHD opened 2 years ago
I assume your pngs contain transparency and no animation frames, therefore I use loadPNG32/savePNG32
pair.
what we do here basically only preserve the pixel data(+ transparency) and discard everything else.
if you also want to discard transparency, you can use loadPNG24/savePNG24
version.
And then nimPNG will try to find the best compression for your new png.
let lres = loadPNG32(seq[uint8], "input.png")
if lres.isOk:
let png = lres.get()
let sres = savePNG32("output.png", png.data, png.width, png.height)
if sres.isErr:
echo sres.error
else:
echo lres.error
Hi, Thanks for your help ! That still produces larger output than gimp Original: 157B GIMP: 103B nimPNG: 134B
then try this. it will take longer time, but produce smaller output.
if it still produce larger size than gimp, then you perhaps should find other png library.
currently nimPNG
doesn't expose it's compression library settings to user, and it requires some work to enable that. and unfortunately I'm still too busy to make such modification.
var conf = makePNGEncoder()
conf.filterStrategy = LFS_BRUTE_FORCE
let sres = savePNG32("output.png", png.data, png.width, png.height, conf)
I think it's not related to compression.
When opening the 3 images in https://www.nayuki.io/page/png-file-chunk-inspector, it shows that gimp only includes IHDR
, IDAT
and IEND
chunks.
while nimPNG also includes PLTE
and tRNS
I see, then you can try to turn off autoConvert
. If autoConvert
enabled, it will count how many colors in the image and create palette if color number <= 256. This strategy works for bigger image to reduce file size. But looks like your image is small enough and this feature actually add more bytes.
var conf = makePNGEncoder()
conf.autoConvert = false
alternative, read important chunk only, and then write back without reencoding.
import ../nimPNG, streams
proc main() =
var conf = makePNGDecoder()
conf.colorConvert = false
conf.readTextChunks = false
conf.rememberUnknownChunks = false
var s = newFileStream("input.png", fmRead)
var png = decodePNG(seq[uint8], s, conf)
s.close()
s = newFileStream("output.png", fmWrite)
png.writeChunks s
s.close()
main()
The first code produces 111B (better), gimp produces 103B. The second one produces a corrupt file.
sorry, out of idea.
Hi, I want to remove all unnecessary data from a png image, I usualy open the image in gimp and export with all options disabled. But now I need to do this for ~200 images, so I wanted to automate this, and I thought about this package.
How can I get the equivalent of gimp's exported png ? Thank you 🙂