jangko / nimPNG

PNG (Portable Network Graphics) decoder and encoder written in Nim
MIT License
90 stars 12 forks source link

How to remove all unnecessary data #66

Open AmjadHD opened 2 years ago

AmjadHD commented 2 years ago

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 ? image Thank you 🙂

jangko commented 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
AmjadHD commented 2 years ago

Hi, Thanks for your help ! That still produces larger output than gimp Original: auto_complete_detail_pane 157B GIMP: auto_complete_detail_pane_gimp 103B nimPNG: auto_complete_detail_pane_nimPNG 134B

jangko commented 2 years ago

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) 
AmjadHD commented 2 years ago

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

jangko commented 2 years ago

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
jangko commented 2 years ago

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()
AmjadHD commented 2 years ago

The first code produces 111B (better), gimp produces 103B. The second one produces a corrupt file.

jangko commented 2 years ago

sorry, out of idea.