Posted By

haxpor on 11/18/11


Tagged

perl pngcrush


Versions (?)

Perl script working with pngcrush, for 1 level of folder hierarchy


 / Published in: Perl
 

URL: http://haxpor.org/post/12923010987/how-to-optimize-applications-file-size-with-free-tools

Wrote this script to work with pngcrush command line based program on Windows system to compress several .png images in multiple folders (with only 1 level of folders, rather than that you have to repeat doing it manually).

It's fast, and help doing the task at hand but it's completely clean.

Please note that the code doesn't take care for all un-expected result, it just does the job in controlled environment.

This script relates to my optimization on application's file size, check it out at the URL above.

  1. #!E:/strawberry/perl/bin/perl
  2. # Handy script for using pngcrush to compress the size of png files in multiple folders
  3.  
  4. $dir = "C:/Users/Haxpor/Desktop/in";
  5. $out = "C:/Users/Haxpor/Desktop/out/";
  6.  
  7. opendir(DIR, $dir) || die("Cannot open directory");
  8. @folders = readdir(DIR);
  9. closedir(DIR);
  10.  
  11. foreach $folder (@folders)
  12. {
  13. # read in each folder
  14. opendir(SUBFOLDER, $dir."/".$folder) || print("Error reading $folder\n");
  15. @files = readdir(SUBFOLDER);
  16. closedir(SUBFOLDER);
  17.  
  18. # create dir in output folder
  19. mkdir($out."/".$folder);
  20.  
  21. # process each file
  22. foreach $file (@files)
  23. {
  24. if(-f $dir."/".$folder."/".$file && $file =~ /.*.png/i)
  25. {
  26. system("pngcrush $dir/$folder/$file $out/$folder/$file");
  27. }
  28. }
  29.  
  30. print "\n";
  31. }

Report this snippet  

You need to login to post a comment.