Today I rewrote my old png optimization batch scripts to be a little more clever and user friendly.
My goal was to make a script that was easy for Joe Average to use. Joe Average doesn't want to wait 20 minutes for pngout to optimize a 1600*1200 truecolor screenshot, so I only used pngrewrite, pngcrush and advpng. You might be able to squeeze a few extra bytes out of the file with other tools, but it's generally not worth the hassle.
I intended to make a different version for people who want to optimize as much as possible regardless of speed, but then I thought, why not make a script that does it all.
So now I present this batch script with an --insane switch, that when invoked runs the mentioned programs plus pngout. If not invoked, it skips pngout.
Read the comments in the script, it needs a tiny bit of configuration. You can drag and drop any number of files(*) on the script, and it will even display progress in the title bar. You need to have everything in the same directory though (that includes the files you are optimizing).
This is for Windows 2000/XP/2003 only, since it uses some advanced scripting commands, only supported on newer versions of Windows.
Download the script and needed programs. Updated August 12, 2005. See below.
Disclaimer: I tested this a lot, and it should be bug free, but make sure you have a backup if you're running this on anything important.
The script does not support wildcards (*.png). Use of wildcards may result is loss of data. I'm fixing this soon. Also see above disclaimer. Fixed.
(*)It's seems Windows is not able to handle command lines exeeding a certain number of characters. So you are limited in the number files you can drop on the script. This limit depends on filename length of course, so there are no set rule about how many you can drop. I currently have no idea how to circumvent this. Nothing bad happens if you try dropping too many files. Windows just throws an error message, and the script doesn't run.
I've decided to put the script here in a codebox, since there still seems to be some interest in it. However, hunting down the required files is left as an exercise for the user, since I don't really want to keep the download package up to date any more.@ECHO OFF
REM PNG.CMD version 06-11-2005-23:27 (MM-DD-YYYY-HH:MM)
REM Latest version available at:
REM [url=http://hydrogenaudio.org/forums/]http://hydrogenaudio.org/forums/[/url][QUESTION MARK]showtopic=22036
REM Created by JensRex (jens@jensrex.net). Feel free to modify
REM and redistribute as you wish. Credit is appreciated.
REM
REM ATTENTION!
REM Specify the complete path to your PNG directory below.
REM This directory must contain pngrewrite.exe, pngcrush.exe,
REM advpng.exe, pngout.exe, zlib.dll and this .cmd file.
REM
REM Example:
REM set pngdir="unconfigured"
REM This setting is case sensitive.
set pngdir="D:\PNG\"
REM No further editing is necessary below this line.
if not %pngdir%==unconfigured goto filecheck
echo.
echo PNG directory not configured. Open this file in a text editor for details.
goto exit
:filecheck
if not .%1==. goto init
echo.
echo Optimizes PNG files.
echo.
echo Usage: PNG.cmd (--insane) [file1] [file2] ...
echo --insane (Optional) Enables extra PNG optimization (pngout.exe).
echo Extremely slow for big files, and generally not worth
echo the effort, unless you really need to push PNG to the
echo limit.
echo.
echo You can drag-and-drop any number of PNG files onto this file
echo to batch optimize them all. The title bar of the command window
echo will display progress information.
goto exit
:init
cd %pngdir%
set pngdir=%pngdir:"=%
%pngdir:~0,2%
REM TODO: Fix filecounting bug with --insane switch.
for %%i in (%*) do set /a totalfiles+=1
:start
set tempfile=
set tempfile=%random%
set /a numpng+=1
title Optimizing file %numpng% of %totalfiles%
if %1==--insane goto renameelse
if %1==*.png (
echo.
echo Wildcards are not supported.
set /a numpng-=1
goto shift1
)
if %~x1==.* (
echo.
echo Wildcards are not supported.
set /a numpng-=1
goto shift1
)
if not %~x1==.png (
echo.
echo ERROR: %1 is not a PNG file.
set /a numpng-=1
goto shift1
)
if not exist %1 (
echo.
echo ERROR: %1 does not exist.
set /a numpng-=1
goto shift1
)
if not "%~dp1"=="%pngdir%" (
echo.
echo ERROR: Input files must be located in same directory as PNG.CMD.
set /a numpng-=1
goto shift1
)
rename %1 %tempfile%.png
goto renameendif
:renameelse
if %2==*.png (
echo.
echo Wildcards are not supported.
set /a numpng-=1
goto shiftelse
)
if %~x2==.* (
echo.
echo Wildcards are not supported.
set /a numpng-=1
goto shiftelse
)
if not %~x2==.png (
echo.
echo ERROR: %2 is not a PNG file.
set /a numpng-=1
goto shiftelse
)
if not exist %2 (
echo.
echo ERROR: %2 does not exist.
set /a numpng-=1
goto shiftelse
)
if not "%~dp1"=="%pngdir%" (
echo.
echo ERROR: Input files must be located in same directory as PNG.CMD.
set /a numpng-=1
goto shiftelse
)
rename %2 %tempfile%.png
:renameendif
pngrewrite.exe %tempfile%.png %tempfile%_pal.png
copy %tempfile%_pal.png %tempfile%.png>nul
pngcrush.exe -brute -l 9 -rem alla %tempfile%.png %tempfile%_opt.png
if %1==--insane pngout.exe %tempfile%_opt.png
advpng.exe -z -4 %tempfile%_opt.png
if %1==--insane goto copyelse
copy %tempfile%_opt.png %1
rename %1 *.png
goto copyendif
:copyelse
copy %tempfile%_opt.png %2
rename %2 *.png
:copyendif
del %tempfile%_pal.png %tempfile%_opt.png %tempfile%.png
if %1==--insane goto shiftelse
:shift1
shift
if .%1==. goto close
goto shiftendif
:shiftelse
shift /2
if .%2 ==. goto close
:shiftendif
goto start
:close
title Optimization complete
echo.
echo PNG optimization complete. Optimized %numpng% files.
set pngdir=
set numpng=
set tempfile=
set totalfiles=
:exit
pause
title %ComSpec%