How to PNGCrush a whole directory tree of PNGs on Windows?
Asked Answered
R

3

5

The question is pretty much what is asked in the title.

I have a lot of PNG files created by MapTiler. 24083 files to be exact. They are within many folders which are in many folders i.e. a tree of folders, duh. Thing is, it's the biggest waste of time to manually PNGCrush all of those.

Does anyone have an algorithm to share for me please? One that could recursively crush all these PNGs?

I have a Windows PC and would love to have it rather in Java or PHP than another language (since I already know it well) But else something else might be fine.

Thanks!

Regen answered 29/6, 2011 at 8:10 Comment(1)
It's not literally impossible to manually PNGCrush all of them. It would take a long time, but that's not the same as it being literally impossible.Cagliostro
P
13

You don't need anything special for this, just use the FOR command in the Windows Command Prompt.

Use this line:

FOR /R "yourdir" %f IN (*.png) DO pngcrush "%f" "%f.crushed.png"

The "yourdir" is the root-directory where the input files are stored.

The two %f's at the end:

  • The first one is the input filename
  • The second one is the output filename

-ow option added in 1.7.22 to make the operation in-place:

FOR /R "yourdir" %f IN (*.png) DO pngcrush -ow "%f"

See this page for more information of FOR.

Phrase answered 29/6, 2011 at 8:22 Comment(4)
@Gerco, would this cover all the png's within multiple folders inside the main folder? (Recursion)Regen
@Nideo: Yes, it's recursive and will process all png files in that directory and all below it.Phrase
@Gerco, can you please help me with setting pngcrush up for CMD usage? It tells me 'pngcrush' is nor recognized... What file must I copy into what folder to make that command exist? (I have no CMD experience, only using 'cd')Regen
Above command was throwing error that it cannot overwrite existing file. I have modified it a bit and works now............................................................................................................................................ FOR /R "E:\workspace\pngs" %f IN (*.png) DO ( E:\workspace\pngcrush\pngcrush.exe -brute "%f" "%f-crushed.png" && move /Y "%f-crushed.png" "%f" )Unlay
A
1

The program 'sweep' http://users.csc.calpoly.edu/~bfriesen/software/files/sweep32.zip lets you run the same command on all files in a directory recursively.

Allista answered 29/6, 2011 at 8:16 Comment(3)
How does sweep work? I can't 2-click on it (it just closes again) and it doesn't work if I drag the main folder into/over it.Regen
It's a command line program, so you need to open cmd and type your stuff in. PNGCrush works from the command line too so you should be fine.Allista
Just checked, it might only run once per directory, so you might need to combine it with the other answer using FORAllista
K
1

See: RecursiveIteratorIterator with RecursiveDirectoryIterator and exec (or similar)

With that you can use:

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('%your-top-directory%'));
foreach ($it as $entry) {
  if (strtolower($entry->getExtension()) == 'png') {
     // execute command here
  }
}
Kaffiyeh answered 29/6, 2011 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.