Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 389
Pangolin Avatar asked Jun 29 '11 08:06

Pangolin


3 Answers

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.

like image 55
Gerco Dries Avatar answered Oct 04 '22 02:10

Gerco Dries


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.

like image 41
artbristol Avatar answered Oct 04 '22 04:10

artbristol


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
  }
}
like image 31
Yoshi Avatar answered Oct 04 '22 04:10

Yoshi