#!/usr/bin/perl # # (c) Copyright 2005, 2007 # Author: Åsmund Ødegård # RJ (contribution through http://billigites.blogspot.com) # # img-focalstat.pl is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # img-focalstat.pl is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with img-focalstat.pl; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA use File::Find; use Getopt::Std; use POSIX; ($#ARGV > -1 and $basedir = $ARGV[0]) or $basedir = "."; getopts('s:b:o:n:ig',\%opts); my $show=0; my $bars=0; my $numbars=10; my $outputfile="bar.png"; my $include_nafocal=0; $opts{s} and $show=1; $opts{b} and $basedir = $opts{b}; $opts{g} and $bars=1; $opts{o} and $outputfile=$opts{o}; $opts{n} and $numbars=$opts{n}; $opts{i} and $include_nafocal=1; #print "base: " . $basedir . "\n"; my %allfocal = (); my %focalcount = (); sub findjpg { my $filename = $_; if ( $filename =~ /jpg$/i ) { $fullfilename = $File::Find::name; #print "Found jpg: " . $filename . " "; #find focal-length: $focal = &findfocal($filename); if ( $include_nafocal or $focal != "n/a" ) { #print "Focal length: ${focal}mm \n"; $focalcount{$focal} and ++$focalcount{$focal} or $focalcount{$focal} = 1; $allfocal{$focal} and $allfocal{$focal} .= ",$fullfilename" or $allfocal{$focal} = "$fullfilename"; #print "So far: " . $focalcount{$focal} . " with this focallength\n"; #print "These are the files: $allfocal{$focal}\n"; } } } sub findfocal { # assume that we have a jpeg here. my $filename = $_; open (EXIF, "jhead \"$filename\"|") or die ("Can't run jhead on file\n"); $focal=""; while () { /^Focal length :\s*(\d+.\d*).*/ and $focal = $1 and last; } close EXIF; $focal == "" and $focal = "n/a"; $focal; } # find with side-effects, set the %allfocal and %focalcount find(\&findjpg,$basedir); foreach $key ( sort { $a <=> $b } keys %focalcount ) { print "$key: $focalcount{$key} pictures\n"; } if ( $show ) { print "Show images with focallength $opts{s}\n"; $cmd = "gthumb "; @imgs = split /,/,$allfocal{$opts{s}}; foreach $img ( @imgs ) { $cmd .= "$img "; } print $cmd . "\n"; system $cmd; } if ( $bars ) { # create a bar-plot using GD my $width = $numbars*55; use GD::Graph::bars; use GD::Graph::Data; use GD::Graph::colour qw(:colours :lists :files :convert); GD::Graph::colour::read_rgb("/etc/X11/rgb.txt") or die "cannot read colors"; my $barplot = GD::Graph::bars->new($width,400); $barplot->set ( x_label => 'focal lengths (in mm)', y_label => 'num pictures', title => 'focal length statistics', transparent => 0, bgclr => 'DimGray', ); my @focals = sort {$a <=> $b } (keys %focalcount); my $minfocal = $focals[0]; my $maxfocal = $focals[$#focals]; #print "minfocal: $minfocal, maxfocal: $maxfocal\n"; my $step = ceil(($maxfocal - $minfocal)/$numbars); #print "Step will be $step\n"; my @xdata; my @ydata; my $tmpfocal = $minfocal; my $focalsptr = 0; while ($tmpfocal <= $maxfocal) { my $upperb = $tmpfocal + $step; #print "Counting from $tmpfocal to $upperb: Adding for "; push @xdata,"$tmpfocal-$upperb"; my $tmpcount = 0; while ($focalsptr <= $#focals and $focals[$focalsptr] <= $upperb) { #print "$focals[$focalsptr],"; $tmpcount += $focalcount{$focals[$focalsptr]}; $focalsptr += 1; } #print ": $tmpcount\n"; push @ydata, $tmpcount; $tmpfocal += $step; } #my @bardata = (@xdata,@ydata); #my @bardata = (["20-30","30-40","40-50","50-60","60-70","70-80","80-90","90-100","100-200","200-300"], # [ @ydata ]); if ( ! -e $outputfile ) { my $bardata = GD::Graph::Data->new( [[@xdata],[@ydata]] ); my $values = $bardata->copy; $barplot->set(show_values => $values); my $plot = $barplot->plot($bardata) or die $barplot->error; open (IMG, ">$outputfile") or die $!; binmode IMG; print IMG $plot->png; close IMG; } else { print "File $outputfile exist, will not overwrite\n"; } }