Sorting images based on their Pixel Dimension. (ver 0.5)

September 16th, 2007

I have a lot of images and almost all of them are saved based on JPEG standard. I have tried different software to sort them based on size but none that I know can do it based on image's pixel dimension(1). JPEG compresses an image based on the color information present in the image (2). Thus, the file sizes are not a true representation of how large an image is when displayed on screen. In trying to sort out small images based on their width or height we face the challenge of sorting images which are unusually tall or wide. Hence, sorting images based on their pixel dimension is a more accurate way of sorting images into groups. I present a script in PHP that does exactly that. The code is fast enough although you need to increase the execution time in php.ini. This script is to be used as a shell script and not in a website. Here are the results I got:
Total of 1019 out of 10000 were small.
Total of 2407 out of 10000 were medium.
Total of 6574 out of 10000 were large.
406.990354061 seconds to sort images

So that is roughly 0.04 seconds for each image.

Listings: HotScript Listed!
px.sklar.com Listed!

PHP:
  1. /*
  2. small :
  3. 150 x 150 or smaller
  4. medium :
  5. larger than 150 x 150 and
  6. smaller than 500 x 500
  7. large :
  8. 500 x 500 or larger
  9. */
  10. function pixel_dim_sort($src_dir, $dest_dir){
  11.     $dir = $src_dir; // directory where the unsorted images are
  12.  
  13.     // $dest_dir  directory where sorted images will be stored
  14.     // in three folders small, mediam and large
  15.  
  16.     if (!file_exists($dir))
  17.     { echo "Source directory $dir does not exist! <br/>\n   "; exit;}
  18.  
  19.     if (!file_exists($dest_dir ))
  20.     mkdir($dest_dir );        // create the sorted images directory if it does not exist
  21.     $dir_small = $dest_dir  . "small/";
  22.     $dir_medium = $dest_dir  . "medium/";
  23.     $dir_large = $dest_dir  . "large/";
  24.  
  25.     $pixel_dim_small = 22500; // x <= 150*150
  26.     $pixel_dim_medium = 250000 ; //500*500<= x <150*150
  27.     $pixel_dim_large = 250000; // x> 500*500
  28.     if (!file_exists($dir_small))
  29.     mkdir($dir_small);
  30.     if (!file_exists($dir_medium))
  31.     mkdir($dir_medium);
  32.     if (!file_exists($dir_large ))
  33.     mkdir($dir_large );
  34.  
  35.     $total_count =0;
  36.     $total_small = 0;
  37.     $total_medium = 0;
  38.     $total_large = 0;
  39.     // Open a known directory, and proceed to read its contents
  40.     if (is_dir($dir)) {
  41.         if ($dh = opendir($dir)) {
  42.             while (($file = readdir($dh)) !== false) {
  43.                 if($file != '.' && $file != '..'){
  44.                     $imagehw = GetImageSize($dir. $file);
  45.                     $total_count++;
  46.                     $imgdim= $imagehw[0]*$imagehw[1];
  47.                     if( $imgdim <= $pixel_dim_small) {
  48.  
  49.                         $bool = copy($dir. $file, $dir_small . $file);
  50.                         if ($bool )  // making sure we don't delete the file which is not copied
  51.                         unlink($dir.$file);
  52.                         else {echo "copy file failed ... "; exit; }
  53.                         $total_small++;
  54.                     }
  55.                     elseif( ($imgdim> $pixel_dim_small) && ($imgdim  <= $pixel_dim_medium) ) {
  56.                         $bool = copy($dir. $file, $dir_medium . $file);
  57.                         if ($bool )
  58.                         unlink($dir.$file);
  59.                         else {echo "copy file failed ... "; exit; }
  60.                         $total_medium++;
  61.                     }
  62.                     elseif($imgdim > $pixel_dim_large) {
  63.                         $bool = copy($dir. $file, $dir_large . $file);
  64.                         if ($bool )
  65.                         unlink($dir.$file);
  66.                         else {echo "copy file failed ... "; exit; }
  67.                         $total_large++;
  68.                     }
  69.                 }
  70.             }
  71.             closedir($dh);
  72.         }
  73.     }
  74.     $total_count = $total_small + $total_medium + $total_large;
  75.     echo "Total of $total_small out of $total_count were small. <br />\n ";
  76.     echo "Total of $total_medium out of $total_count were medium. <br />\n ";
  77.     echo "Total of $total_large out of $total_count were large. <br />\n ";
  78. }
  79.  
  80. $start = time() + microtime();
  81.  
  82. pixel_dim_sort("f:/0000/new/", "f:/0000/sorted2/");
  83.  
  84. echo time() + microtime() - $start, ' seconds to sort images.';

Close
E-mail It