Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've been trying to find png image files a certain height (over 500px). I know that file will return image dimensions. Example:

$ file TestImg1a.png

TestImg1a.png: PNG image data, 764 x 200, 4-bit colormap, non-interlaced   

But I need to use this to find all files in a directory with a height over 500px. I know how to print out all files regardless of height:

find . -name '*.png' | xargs file | awk '{print $7 " " $1}'

But how do I limit the $7 to those results greater than 500?

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

i know this is a bit overkill but, this will work every time (even if there are spaces in your filename) and regardless of how file displays the information.

find . -name '*.png' -exec file {} \; | sed 's/\(.*png\): .* \([0-9]* x [0-9]*\).*/\2 \1/' | awk 'int($1) > 500 {print}'

and it prints the dimensions of the picture and the file

explaination:

  1. find all files named *.png under . and for each do a file on it

  2. use sed to print only the filename and dimensions then re-order to print dimensions first

  3. use awk to test the first number (height of pic) making sure its greater than 500 and if it is print dimensions and file name, if not do nothing.

share|improve this answer
    
Thanks. I did have to make a slight change -- the $1 in the awk argument to $3. But this definitely got it for me. –  steve-er-rino Oct 26 '12 at 10:57
add comment

I feel that something other than shell utilities would be more appropriate, e.g., Perl:

#!/usr/bin/perl

use File::Find;
use Image::Info qw(image_info dim);

find (\&check_height, './');

sub check_height {

  my $info = image_info( $_ );
  my ($width, $height) = dim( $info );
  print $_ . " has height $height\n" if ( $height > 500 );

}

Less dicking around with trying to parse out $7; just get the dimensions directly. Yes, you'll need the Image::Info module, but, on CentOS/RHEL, it's a standard package, so you can just run yum install perl-Image-Info.

share|improve this answer
1  
While perl is a normally a great solution, it's not in this case, esp since I don't have Image::Info nor the option to install. –  steve-er-rino Oct 26 '12 at 10:59
    
The perl solution is somewhat faster than the find/file/awk one, which is nice, and on ubuntu derivates the image info module is available from apt-get install libimage-info-perl –  rivimey Jan 4 at 14:44
add comment
exiftool -q -r -ext png -if '$ImageHeight > 500' -p '$Directory/$FileName' .
share|improve this answer
add comment
find . -name '*.png' -exec file "{}" \+ | awk -F"(: +PNG image data|,| x )" '$4 > 500 {print $4" "$1}'
share|improve this answer
    
doesn't work: 96, ./4/45445106_w185.png: 86, ./4/404358x_w185.png: 86, ./4/404341x_w185.png: 80, ./4/475986_w185.png: 621, ./4/481693_w185.png: 667, ./4/42513x_w185.png: 86, ./4/404372x_w185.png: –  steve-er-rino Oct 25 '12 at 18:58
    
@tink, cast $7 to an int before comparison, i.e. int($7) > 500.. in the absence of casting awk is resorting to a literal string compare –  1_CR Oct 25 '12 at 19:49
    
Steve, where does that "," come from? My "file" doesn't produce that. But as Chandra said: you can explicitly force $7 to become an integer using the method pointed out in his comment. –  tink Oct 25 '12 at 19:54
    
your awk statement will only work if the filename has no spaces in it –  h3rrmiller Oct 25 '12 at 20:04
    
@tink the "," is part of what file returns in my environment. –  steve-er-rino Oct 26 '12 at 11:01
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.