Skip to content

Instantly share code, notes, and snippets.

@Olathe
Last active December 19, 2015 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Olathe/5871420 to your computer and use it in GitHub Desktop.
Save Olathe/5871420 to your computer and use it in GitHub Desktop.
Procedure for optimizing PNGs
  1. pngcheck can detect non-PNGs:
    find -name '*.png' -exec pngcheck -q '{}' +
  2. GIMP GUI (unlike *Magick or Script-Fu, which are buggy) can properly convert to sRGB (remember to overwrite the original for each one):
  • Open in GIMP (press Ctrl-C once it stops opening windows for a while):
    find -name '*.png' -exec sh -c "pngcheck -v '{}' | grep -q 'chunk iCCP' && echo \"\\\"{}\\\"\"" \; | xargs gimp & while true; do window_id=$(wmctrl -l | grep -Pom 1 "^(0x[0-9a-f]{8})(?=\s.+ Convert to RGB working space\?$)"); if [ "$window_id" != "" ]; then xsendkey -window $window_id "Alt+C"; fi; sleep 1; done
  • Convert and overwrite originals:
    wmctrl -l | grep -Po "^(0x[0-9a-f]{8})(?=\s.+ GIMP$)" | while read window_id; do echo Handling window $window_id; xsendkey -window $window_id Alt+i; xsendkey -window $window_id m; xsendkey -window $window_id c; until (wmctrl -l | grep -Poq "^(0x[0-9a-f]{8})(?=\s.+ Convert to ICC Color Profile$)"); do sleep 1; done; wmctrl -l | grep -Po "^(0x[0-9a-f]{8})(?=\s.+ Convert to ICC Color Profile$)" | while read dialog_id; do echo Handling dialog $dialog_id; wmctrl -iR $dialog_id; xsendkey -window $dialog_id Alt+b; xsendkey -window $dialog_id Alt+b; xsendkey -window $dialog_id Down; xsendkey -window $dialog_id Right; xsendkey -window $dialog_id space; while (wmctrl -l | grep -q $dialog_id); do sleep 1; done; done; xsendkey -window $window_id Alt+f; xsendkey -window $window_id w; sleep 1; xsendkey -window $window_id Control+z; wmctrl -ic $window_id; done
  1. My custom script for the Linux version of PNGOUT can find the best PNGOUT settings and make a second script that repeatedly tries random Huffman tables with them.
  2. Once that's done, run AdvPNG 1.17 or later:
    find -name '*.png' | xargs -P $(grep -c ^processor /proc/cpuinfo) -n 1 -I{} advpng -z -3 -i 255 '{}'
    find -name '*.png' | xargs -P $(grep -c ^processor /proc/cpuinfo) -n 1 -I{} advpng -z -4 -i 10000 '{}'
  3. Once that's done, run PNGOUT repeatedly:
    ./compress-all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment