Another PNG optimization script |
- No Warez. This includes warez links, cracks and/or requests for help in getting illegal software or copyrighted music tracks!
- No Spamming or Trolling on the boards, this includes useless posts, trying to only increase post count or trying to deliberately create a flame war.
- No Hateful or Disrespectful posts. This includes: bashing, name-calling or insults directed at a board member.
- Click here for complete Hydrogenaudio Terms of Service
![]() ![]() |
Another PNG optimization script |
May 28 2004, 00:26
Post
#1
|
|
![]() Group: Super Moderator Posts: 589 Joined: 18-December 01 From: Denmark Member No.: 680 |
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. 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. (*)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 @ECHO OFF
REM PNG.CMD version 06-11-2005-23:27 (MM-DD-YYYY-HH:MM) REM Latest version available at: REM http://hydrogenaudio.org/forums/[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% This post has been edited by JensRex: May 5 2008, 22:15 |
|
|
|
Jan 23 2005, 21:13
Post
#2
|
|
![]() Group: Super Moderator Posts: 589 Joined: 18-December 01 From: Denmark Member No.: 680 |
Due to the overwhelming feedback (
Removed: download link is in first post. This post has been edited by JensRex: Jun 3 2005, 23:33 |
|
|
|
Jan 23 2005, 21:59
Post
#3
|
|
![]() A/V Moderator Group: Moderator Posts: 1526 Joined: 30-April 02 From: Slovenia Member No.: 1922 |
<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 |
|
|
|
Jan 23 2005, 22:06
Post
#4
|
|
![]() Group: Super Moderator Posts: 589 Joined: 18-December 01 From: Denmark Member No.: 680 |
QUOTE (smok3 @ Jan 23 2005, 09:59 PM) <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> 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). |
|
|
|
Jan 23 2005, 22:49
Post
#5
|
|
![]() Musepack Developer Group: Members Posts: 359 Joined: 17-October 01 Member No.: 309 |
I heard ImageMagick could serve as whatever2png (whatever2whatever actually): http://www.imagemagick.org/
|
|
|
|
Jan 23 2005, 22:56
Post
#6
|
|
![]() A/V Moderator Group: Moderator Posts: 1526 Joined: 30-April 02 From: Slovenia Member No.: 1922 |
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 |
|
|
|
Jan 23 2005, 23:01
Post
#7
|
|
![]() Group: Super Moderator Posts: 589 Joined: 18-December 01 From: Denmark Member No.: 680 |
Updated script to use randomly named temporary files.
|
|
|
|
Jan 24 2005, 00:41
Post
#8
|
|
![]() Group: Super Moderator Posts: 589 Joined: 18-December 01 From: Denmark Member No.: 680 |
I made one script for converting various image types to PNG. It looks like this:
CODE @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. This post has been edited by JensRex: Jan 24 2005, 00:46 |
|
|
|
Jan 24 2005, 14:05
Post
#9
|
|
![]() Group: Super Moderator Posts: 589 Joined: 18-December 01 From: Denmark Member No.: 680 |
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. |
|
|
|
Apr 15 2005, 15:38
Post
#10
|
|
![]() Group: Members Posts: 1187 Joined: 3-September 03 From: Bergen, Norway Member No.: 8667 |
Hey, I really would want this.
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 |
|
|
|
Apr 15 2005, 17:48
Post
#11
|
|
![]() Group: Members Posts: 1187 Joined: 3-September 03 From: Bergen, Norway Member No.: 8667 |
Another intreresting
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 |
|
|
|
Apr 16 2005, 11:18
Post
#12
|
|
![]() Group: Super Moderator Posts: 589 Joined: 18-December 01 From: Denmark Member No.: 680 |
QUOTE (Mr_Rabid_Teddybear @ Apr 15 2005, 03:38 PM) 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. |
|
|
|
Apr 16 2005, 16:01
Post
#13
|
|
![]() Group: Members Posts: 1187 Joined: 3-September 03 From: Bergen, Norway Member No.: 8667 |
QUOTE (JensRex @ Apr 16 2005, 02:18 AM) Wildcards are not supported. I should have written that somewhere. Sorry about that. I may look into adding support for wildcards later. 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 |
|
|
|
Apr 17 2005, 01:03
Post
#14
|
|
![]() Group: Super Moderator Posts: 589 Joined: 18-December 01 From: Denmark Member No.: 680 |
|
|
|
|
May 24 2005, 08:49
Post
#15
|
|
![]() Group: Members Posts: 1045 Joined: 28-June 03 From: on the dock of the bay Member No.: 7423 |
QUOTE (JensRex @ Apr 17 2005, 01:03 AM) QUOTE (Mr_Rabid_Teddybear @ Apr 16 2005, 04:01 PM) 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.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 ;)
|
|
|
|
May 24 2005, 16:00
Post
#16
|
|
![]() Group: Super Moderator Posts: 589 Joined: 18-December 01 From: Denmark Member No.: 680 |
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. This post has been edited by JensRex: May 24 2005, 16:03 |
|
|
|
May 24 2005, 16:34
Post
#17
|
|
![]() Group: Members Posts: 1045 Joined: 28-June 03 From: on the dock of the bay Member No.: 7423 |
QUOTE (JensRex @ May 24 2005, 04:00 PM) 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 ;)
|
|
|
|
Jun 2 2005, 11:30
Post
#18
|
|
|
Group: Members Posts: 29 Joined: 28-May 05 Member No.: 22368 |
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
This post has been edited by miniml: Jun 2 2005, 11:30 |
|
|
|
Jun 3 2005, 22:55
Post
#19
|
|
![]() Group: Super Moderator Posts: 589 Joined: 18-December 01 From: Denmark Member No.: 680 |
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. Edit: Forgot to mention that the download contains latest version of all programs as of today. This post has been edited by JensRex: Jun 3 2005, 23:32 |
|
|
|
Jun 4 2005, 05:47
Post
#20
|
|
![]() Group: Members Posts: 1045 Joined: 28-June 03 From: on the dock of the bay Member No.: 7423 |
thx for the update JensRex. works fine here, going to test it a little more.
-------------------- Nothing but a Heartache - Since I found my Baby ;)
|
|
|
|
Jun 10 2005, 19:51
Post
#21
|
|
![]() Group: Members Posts: 1187 Joined: 3-September 03 From: Bergen, Norway Member No.: 8667 |
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 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 http://hydrogenaudio.org/forums/[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 |
|
|
|
Jun 10 2005, 20:15
Post
#22
|
|
|
Burrrn developer Group: Developer Posts: 916 Joined: 25-November 01 From: Bratislava, Slovakia Member No.: 534 |
QUOTE (Mr_Rabid_Teddybear @ Jun 10 2005, 07:51 PM) 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. You are missing a backslash: set pngdir="F:\screenshots\PNGCompression\" -------------------- Burrrn - http://www.burrrn.net/
MPEG Audio Collection - http://mac.sourceforge.net/ |
|
|
|
Jun 10 2005, 21:24
Post
#23
|
|
![]() Group: Members Posts: 1187 Joined: 3-September 03 From: Bergen, Norway Member No.: 8667 |
QUOTE (Gambit @ Jun 10 2005, 11:15 AM) You're right! Thanks! -------------------- "ONLY THOSE WHO ATTEMPT THE IMPOSSIBLE WILL ACHIEVE THE ABSURD"
- Oceania Association of Autonomous Astronauts |
|
|
|
Jun 10 2005, 21:41
Post
#24
|
|
![]() Group: Members Posts: 1045 Joined: 28-June 03 From: on the dock of the bay Member No.: 7423 |
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 ;)
|
|
|
|
Jun 11 2005, 05:48
Post
#25
|
|
![]() Group: Members Posts: 1187 Joined: 3-September 03 From: Bergen, Norway Member No.: 8667 |
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 |
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 9th February 2010 - 00:21 |