png_recompress.sh 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/bash
  2. # $Id$
  3. # Copyright (C) 2007-2008 Arvid Norlander <anmaster AT tele2 DOT se>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # This script recompresses .png files using optipng and
  19. # advpng to get the smallest images. All recompression is
  20. # lossless.
  21. #
  22. # This script needs at least bash3, bash2 will not work
  23. #
  24. # TODO:
  25. # * Make it work recursively on a directory.
  26. # Check for new enough bash version
  27. fail_old_bash() {
  28. echo "Sorry your bash version is too old!"
  29. echo "You need at least version 3.0 of bash."
  30. echo "Please install a newer version:"
  31. echo " * Either use your distro's packages."
  32. echo " * Or see http://www.gnu.org/software/bash/"
  33. exit 2
  34. }
  35. # Check bash version. We need at least 3.1
  36. # Lets not use anything like =~ here because
  37. # that may not work on old bash versions.
  38. if [[ "${BASH_VERSINFO[0]}" -lt 3 ]]; then
  39. fail_old_bash
  40. fi
  41. if [[ -z "$1" ]] || [[ "$1" == "--help" ]]; then
  42. echo "Usage: $(basename $0) files..."
  43. exit 1
  44. fi
  45. if ! type optipng > /dev/null 2>&1; then
  46. echo "Can't find optipng!"
  47. echo "This script depends on the optipng tool to be in PATH."
  48. echo "Please install it or, if it is already installed add the"
  49. echo "directory it is in to PATH and try again."
  50. echo "Homepage of this tool is: http://optipng.sourceforge.net/"
  51. exit 1
  52. fi
  53. if ! type advpng > /dev/null 2>&1; then
  54. echo "Can't find advpng!"
  55. echo "This script depends on the advpng tool to be in PATH."
  56. echo "Please install it or, if it is already installed add the"
  57. echo "directory it is in to PATH and try again."
  58. echo "Homepage of this tool is: http://advancemame.sourceforge.net/comp-readme.html"
  59. echo "Hint: For package name in your distro, try looking for \"advancecomp\"."
  60. exit 1
  61. fi
  62. echo -e "Please wait, this can take a \e[1mlong\e[0m time."
  63. dooptipng() {
  64. optipng -i 0 -o 7 "$@" | \
  65. awk '
  66. /^\*\* Processing:/ { print "\nFile: " $3 }
  67. /^Input file size/ { print "Input: " $5,$6 }
  68. /^Output file size/ { print "Output: " $5,$6,$7,$8,$9,$10,$11 }
  69. /is already optimized/ { print "Output: No change" }
  70. '
  71. }
  72. doadvpng() {
  73. echo " In Out % Filename"
  74. advpng -z -4 "$@"
  75. }
  76. echo -e "\n\n\n\e[1mPass 1: optipng\e[0m\n\n\n"
  77. dooptipng "$@"
  78. echo -e "\n\n\n\e[1mPass 2: advpng\e[0m\n\n\n"
  79. doadvpng "$@"
  80. echo -e "\n\n\n\e[1mPass 3: optipng again (as advpng often makes optipng more effective)\e[0m\n\n\n"
  81. dooptipng "$@"