Sorting images based on their Pixel Dimension. (ver 0.5)
September 16th, 2007I 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!
-
/*
-
small :
-
150 x 150 or smaller
-
medium :
-
larger than 150 x 150 and
-
smaller than 500 x 500
-
large :
-
500 x 500 or larger
-
*/
-
function pixel_dim_sort($src_dir, $dest_dir){
-
$dir = $src_dir; // directory where the unsorted images are
-
-
// $dest_dir directory where sorted images will be stored
-
// in three folders small, mediam and large
-
-
-
$dir_small = $dest_dir . "small/";
-
$dir_medium = $dest_dir . "medium/";
-
$dir_large = $dest_dir . "large/";
-
-
$pixel_dim_small = 22500; // x <= 150*150
-
$pixel_dim_medium = 250000 ; //500*500<= x <150*150
-
$pixel_dim_large = 250000; // x> 500*500
-
-
$total_count =0;
-
$total_small = 0;
-
$total_medium = 0;
-
$total_large = 0;
-
// Open a known directory, and proceed to read its contents
-
if($file != '.' && $file != '..'){
-
$total_count++;
-
$imgdim= $imagehw[0]*$imagehw[1];
-
if( $imgdim <= $pixel_dim_small) {
-
-
if ($bool ) // making sure we don't delete the file which is not copied
-
$total_small++;
-
}
-
elseif( ($imgdim> $pixel_dim_small) && ($imgdim <= $pixel_dim_medium) ) {
-
if ($bool )
-
$total_medium++;
-
}
-
elseif($imgdim > $pixel_dim_large) {
-
if ($bool )
-
$total_large++;
-
}
-
}
-
}
-
}
-
}
-
$total_count = $total_small + $total_medium + $total_large;
-
echo "Total of $total_small out of $total_count were small. <br />\n ";
-
echo "Total of $total_medium out of $total_count were medium. <br />\n ";
-
echo "Total of $total_large out of $total_count were large. <br />\n ";
-
}
-
-
-
pixel_dim_sort("f:/0000/new/", "f:/0000/sorted2/");
-
