Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Another PNG optimization script (Read 31687 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Another PNG optimization script

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.
Code: [Select]
@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%

Another PNG optimization script

Reply #1
Due to the overwhelming feedback () I got from this script, I've decided to upload the latest version of the script, along with most recent versions of the needed executables to:

Removed: download link is in first post.

Another PNG optimization script

Reply #2
<unrelated> i need some sort of all2png converter, any recommendations? (have terabytes of tga, tiff anims rendered and they are waiting to get a lil smaller - almost all are 32 bit and i want to keep the alpha in pngs as well.)</unrelated>
PANIC: CPU 1: Cache Error (unrecoverable - dcache data) Eframe = 0x90000000208cf3b8
NOTICE - cpu 0 didn't dump TLB, may be hung

Another PNG optimization script

Reply #3
Quote
<unrelated> i need some sort of all2png converter, any recommendations? (have terabytes of tga, tiff anims rendered and they are waiting to get a lil smaller - almost all are 32 bit and i want to keep the alpha in pngs as well.)</unrelated>
[a href="index.php?act=findpost&pid=267494"][{POST_SNAPBACK}][/a]

You should be able to find command line programs to do that. Usually a search for "[whatever]2PNG" should turn up something useful. Then you can write some simple batch script to automate the process.

I wrote batch scripts to do gif2png and bmp2png, but they're old and outdated, and I haven't bothered to write some new. When I get sufficiently bored, I might do that (like I did with the above script).

Another PNG optimization script

Reply #4
I heard ImageMagick could serve as whatever2png (whatever2whatever actually): http://www.imagemagick.org/

Another PNG optimization script

Reply #5
tnx for the answers, meantime i figured xnview will probably do just fine
PANIC: CPU 1: Cache Error (unrecoverable - dcache data) Eframe = 0x90000000208cf3b8
NOTICE - cpu 0 didn't dump TLB, may be hung

Another PNG optimization script

Reply #6
Updated script to use randomly named temporary files.

Another PNG optimization script

Reply #7
I made one script for converting various image types to PNG. It looks like this:

Code: [Select]
@echo off
:start

if /i %~x1==.tiff goto tiff
if /i %~x1==.tif goto tiff
if /i %~x1==.gif goto gif
if /i %~x1==.bmp goto bmp
goto unknown

:tiff
tiff2png.exe -compression 9 %1
call png.cmd %~n1.png
goto end

:gif
gif2png.exe -apt %1
call png.cmd %~n1.png
goto end

:bmp
bmp2png.exe -9 %1
call png.cmd %~n1.png
goto end

:unknown
echo The filetype %~x1 is not supported.
goto end

:end
shift
if .%1==. goto close
goto start

:close

Save it to a file called all2png.CMD, or whatever you want to call it. Then just drag and drop files onto it. Notice the source files has to be in the same directory as the script.

The script calls png.cmd, so you have to remove the pause command from the end of the png.cmd file, or the script will pause after every file it processes.

Get TIFF2PNG here.
Get GIF2PNG here.
Get BMP2PNG here.

Another PNG optimization script

Reply #8
Added safety checks to the script. It will now not try to process files that aren't .png files. It also checks if the file exists before trying to do anything. And finally, the script will now complain if the input files aren't in the same directory as png.cmd and the executables.

Progress indicator now displays "Optimizing file X of Y" instead of just "Optimizing file X".

Clarified help texts.

Updated download links in first post.

Another PNG optimization script

Reply #9
Hey, I really would want this.  I've used the earlier one, but the option to choose whether or not to use pngout would be tremendous.
However, I can't make it work... I've done as instructed, put all the files from png.rar in the same folder, and edited the png.cmd to point to the same folder. The only alteration I've done are changing
set pngdir=unconfigured
to
set pngdir="F:\My Documents\screenshots\PNG-Compression\"

When I try to run it, all the output I get are:
Quote
F:\My Documents\screenshots\PNG-Compression>png.cmd 215680296.png
Documents\screenshots\PNG-Compression\==F:\My was unexpected at this time.

Can't it cope with spaces in path, even when enclosing in quotation marks, or...?

Yes, this was really the problem, when changing to
set pngdir="F:\screenshots\PNG-Compression\"
it works....
Well, I just report anyhow...
"ONLY THOSE WHO ATTEMPT THE IMPOSSIBLE WILL ACHIEVE THE ABSURD"
        - Oceania Association of Autonomous Astronauts

Another PNG optimization script

Reply #10
Another intreresting  thing about this script is that it seems to work nice if you drag the file onto png.cmd or you run it in CMD with filename specified. Well, I also tried to run it with wildcard; png.cmd *.png... It ran, optimized the file, and then deleted it (and not to the recycle bin)! This was the concluding message it gave:
Quote
      458020      458020 100% 29908_opt.png (Bigger 667976(a3148h))
      458020      458020 100%
The file cannot be copied onto itself.
        0 file(s) copied.

PNG optimization complete. Optimized 1 files.

...and the file was gone.... OK. Best to keep backup anyway, I guess 
Otherwise this seems much more efficient than what I used before...
"ONLY THOSE WHO ATTEMPT THE IMPOSSIBLE WILL ACHIEVE THE ABSURD"
        - Oceania Association of Autonomous Astronauts

Another PNG optimization script

Reply #11
Quote
Can't it cope with spaces in path, even when enclosing in quotation marks, or...?[a href="index.php?act=findpost&pid=290768"][{POST_SNAPBACK}][/a]

That's odd. I believe I tested it with spaces in path. But it looks like there's a problem with it. I will take a look at it.

Wildcards are not supported. I should have written that somewhere. Sorry about that. I may look into adding support for wildcards later.

Another PNG optimization script

Reply #12
Quote
Wildcards are not supported. I should have written that somewhere. Sorry about that. I may look into adding support for wildcards later.
[a href="index.php?act=findpost&pid=291012"][{POST_SNAPBACK}][/a]

Well, I tested it out, so no important file was lost in the process
But wouldn't it be wise if the script refused to take wildcards at all as long as not supported (if that's possible to arrange), instead of described behaviour, so that some innocent soul doesn't one day inadvertently delete something that was really important for her/him....?
"ONLY THOSE WHO ATTEMPT THE IMPOSSIBLE WILL ACHIEVE THE ABSURD"
        - Oceania Association of Autonomous Astronauts

Another PNG optimization script

Reply #13
Quote
But wouldn't it be wise if the script refused to take wildcards at all as long as not supported...[a href="index.php?act=findpost&pid=291062"][{POST_SNAPBACK}][/a]

Yes, indeed it would. I will look into this very soon.

Another PNG optimization script

Reply #14
Quote
Quote
But wouldn't it be wise if the script refused to take wildcards at all as long as not supported...
Yes, indeed it would. I will look into this very soon.
any news on this one?
also, is your download area down or removed? I can't acces the files anymore and get a 404 for http://jensrex.net/download
ta.
Nothing but a Heartache - Since I found my Baby ;)

Another PNG optimization script

Reply #15
Whoops. Never trust me when I say I will do something soon .

I've found some ways to improve the compression level of the --insane switch quite a lot in some cases, due to the (retarded) way in which advpng does stuff. Basically, consider the --insane switch broken until further notice. The graceful failure of wildcards is also on the todo.

My server has been up and down a lot lately. I have another host (stable and on >1gb) where I will put download stuff. I need to work out some DNS issuses first.

I'll take a look at all this very soon .

Edit: By the way, the other day I tried to drag 79 files onto the script, and Windows threw me an error. It appears there's some limit on the length of supported command lines. Nothing I can do about that.

Another PNG optimization script

Reply #16
Quote
Whoops. Never trust me when I say I will do something soon .
so what makes you think your senseless babbling will only slightly easy my mind on this topic, huh?! enough is enough, you poisoned the PNG world long enough my friend... or should I say fiend?! yeeehhs, I found you! you thought you could hide here at HA and I wouldn't find you? did you? ah well NOW the time has finaly come to...
uhm... to thank you I guess for your little script         

I'm looking forward to a new version 
Nothing but a Heartache - Since I found my Baby ;)

Another PNG optimization script

Reply #17
The download link is dead for me too. I can't wait for the new version (any version, because I didn't managed to download it when the server was up ) and new server.

Another PNG optimization script

Reply #18
Updated PNG script with the following changes.

Changed the order in which the programs are run. Advpng.exe is now run AFTER pngout.exe, because advpng.exe always writes PNG files with filter method 5, regardless of input filter type. Pngout.exe recompresses files with the same filter type as the input file. So that means that if pngcrush decided that filter method 1 was best for a given file, and advpng was able to optimize it further, it would be written with filter method 5. This is fine if pngout.exe isn't invoked, but if it is, pngout will try to optimize using filter method 5, which is not optimal. So this is why pngout.exe wasn't usually able to improve compression. After I discovered this behavior, I'm now a little happier with pngout, since it is very often able to achieve dramatic changes in file size. It is still slow as hell however.

The script should now fail gracefully when attemptning to use wildcards.

Fixed some issues with spaces in directory names.

Reworked sanity checking a little. End result is still the same, but it was giving improper error messages in some caes.

Added note that pngdir variable is case sensitive.

Download links are in first post.

Downloads are now hosted on a different server, and should now stay up unless a meteor his the Copenhagen data center. I'm going to shoot someone. Temporarily hosted on foobar2000.org, thanks Peter.

Edit: Forgot to mention that the download contains latest version of all programs as of today.

Another PNG optimization script

Reply #19
thx for the update JensRex. works fine here, going to test it a little more.
Nothing but a Heartache - Since I found my Baby ;)

Another PNG optimization script

Reply #20
This doesn't work at all here. I've added all files and a test.png to path F:\screenshots\PNGCompression and changed the line in png.cmd to
set pngdir="F:\screenshots\PNGCompression"
but all that happens is that it spits out the below posted messages in CMD. I repeat: png.cmd, all files and the png file are in same folder.

Code: [Select]

F:\screenshots\PNGCompression>png.cmd test.png

F:\screenshots\PNGCompression>rem @ECHO OFF

F:\screenshots\PNGCompression>REM PNG.CMD version 06-03-2005-22:38 (MM-DD-YYYY-H
H:MM)

F:\screenshots\PNGCompression>REM Latest version available at:

F:\screenshots\PNGCompression>REM [url=http://hydrogenaudio.org/forums/]http://hydrogenaudio.org/forums/[/url][QUESTION MARK
]showtopic=22036

F:\screenshots\PNGCompression>REM Created by JensRex (jens@jensrex.net). Feel fr
ee to modify

F:\screenshots\PNGCompression>REM and redistribute as you wish. Credit is apprec
iated.

F:\screenshots\PNGCompression>REM

F:\screenshots\PNGCompression>REM ATTENTION!

F:\screenshots\PNGCompression>REM Specify the complete path to your PNG director
y below.

F:\screenshots\PNGCompression>REM This directory must contain pngrewrite.exe, pn
gcrush.exe,

F:\screenshots\PNGCompression>REM advpng.exe, pngout.exe, zlib.dll and this .cmd
 file.

F:\screenshots\PNGCompression>REM

F:\screenshots\PNGCompression>REM Example:

F:\screenshots\PNGCompression>REM set pngdir="D:\My PNG Files\"

F:\screenshots\PNGCompression>REM This setting is case sensitive.

F:\screenshots\PNGCompression>set pngdir="F:\screenshots\PNGCompression"

F:\screenshots\PNGCompression>REM No further editing is necessary below this lin
e.

F:\screenshots\PNGCompression>if not "F:\screenshots\PNGCompression" == unconfig
ured goto filecheck

F:\screenshots\PNGCompression>if not .test.png == . goto init

F:\screenshots\PNGCompression>cd "F:\screenshots\PNGCompression"

F:\screenshots\PNGCompression>set pngdir=F:\screenshots\PNGCompression

F:\screenshots\PNGCompression>F:

F:\screenshots\PNGCompression>REM TODO: Fix filecounting bug with --insane switc
h.

F:\screenshots\PNGCompression>for %i in (test.png) do set /a totalfiles+=1

F:\screenshots\PNGCompression>set /a totalfiles+=1

F:\screenshots\PNGCompression>set tempfile=

F:\screenshots\PNGCompression>set tempfile=28569

F:\screenshots\PNGCompression>set /a numpng+=1

F:\screenshots\PNGCompression>title Optimizing file 1 of 1

F:\screenshots\PNGCompression>if test.png == --insane goto renameelse

F:\screenshots\PNGCompression>if .png == .* (
echo.
 echo Wildcards are not supported.
 set /a numpng-=1
 goto shift1
)

F:\screenshots\PNGCompression>if not .png == .png (
echo.
 echo ERROR: test.png is not a PNG file.
 set /a numpng-=1
 goto shift1
)

F:\screenshots\PNGCompression>if not exist test.png (
echo.
 echo ERROR: test.png does not exist.
 set /a numpng-=1
 goto shift1
)

F:\screenshots\PNGCompression>if not "F:\screenshots\PNGCompression\" == "F:\scr
eenshots\PNGCompression" (
echo.
 echo ERROR: Input files must be located in same directory as PNG.CMD.
 set /a numpng-=1
 goto shift1
)

ERROR: Input files must be located in same directory as PNG.CMD.

F:\screenshots\PNGCompression>shift

F:\screenshots\PNGCompression>if . == . goto close

F:\screenshots\PNGCompression>title Optimization complete

F:\screenshots\PNGCompression>echo.


F:\screenshots\PNGCompression>echo PNG optimization complete. Optimized 0 files.

PNG optimization complete. Optimized 0 files.

F:\screenshots\PNGCompression>set pngdir=

F:\screenshots\PNGCompression>set numpng=

F:\screenshots\PNGCompression>set tempfile=

F:\screenshots\PNGCompression>set totalfiles=

F:\screenshots\PNGCompression>pause
Press any key to continue . . .

F:\screenshots\PNGCompression>title C:\WINDOWS\system32\cmd.exe

F:\screenshots\PNGCompression>
"ONLY THOSE WHO ATTEMPT THE IMPOSSIBLE WILL ACHIEVE THE ABSURD"
        - Oceania Association of Autonomous Astronauts

Another PNG optimization script

Reply #21
Quote
This doesn't work at all here. I've added all files and a test.png to path F:\screenshots\PNGCompression and changed the line in png.cmd to
set pngdir="F:\screenshots\PNGCompression"
but all that happens is that it spits out the below posted messages in CMD. I repeat: png.cmd, all files and the png file are in same folder.
[a href="index.php?act=findpost&pid=305146"][{POST_SNAPBACK}][/a]


You are missing a backslash:
set pngdir="F:\screenshots\PNGCompression\"

Another PNG optimization script

Reply #22
Quote
You are missing a backslash:
set pngdir="F:\screenshots\PNGCompression\"
[a href="index.php?act=findpost&pid=305150"][{POST_SNAPBACK}][/a]

You're right! Thanks! 
"ONLY THOSE WHO ATTEMPT THE IMPOSSIBLE WILL ACHIEVE THE ABSURD"
        - Oceania Association of Autonomous Astronauts

Another PNG optimization script

Reply #23
I played around with the script a bit more, the --insane switch works perfectly here (though only saves ~0,3KB with my testfile):
original: 32,2 KB
normal: 15,7 KB
insane: 15,4 KB

as a sidenote, am I right in guessing that a big part of the compression is reached in reducing the colors?
Nothing but a Heartache - Since I found my Baby ;)

Another PNG optimization script

Reply #24
Hmmm... Tested, just for the hell of it: You are still allowed to permanently delete your files by running png.cmd *.png
"ONLY THOSE WHO ATTEMPT THE IMPOSSIBLE WILL ACHIEVE THE ABSURD"
        - Oceania Association of Autonomous Astronauts