Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to remove all unnecessary data #66

Open
AmjadHD opened this issue Feb 12, 2022 · 8 comments
Open

How to remove all unnecessary data #66

AmjadHD opened this issue Feb 12, 2022 · 8 comments

Comments

@AmjadHD
Copy link

AmjadHD commented Feb 12, 2022

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
Copy link
Owner

jangko commented Feb 13, 2022

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
Copy link
Author

AmjadHD commented Feb 13, 2022

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
Copy link
Owner

jangko commented Feb 13, 2022

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
Copy link
Author

AmjadHD commented Feb 13, 2022

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
Copy link
Owner

jangko commented Feb 14, 2022

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
Copy link
Owner

jangko commented Feb 14, 2022

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
Copy link
Author

AmjadHD commented Feb 15, 2022

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

@jangko
Copy link
Owner

jangko commented Feb 15, 2022

sorry, out of idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants