Perl Photo Tools - Part 1. Filenames
Apr 19 2007 Programming

Key to these tools are pristine descriptive filenames.
In this post I set the modification times, and gather statistics.  

The most time consuming part of all these tools was scanning, cropping, and naming the files. I asked my daughter to help and do a few each day. Then i asked my eldest son, then my wife. Eventually I had to do them all myself. I love my family but I'm getting off topic.
Here's an example filename.
"eric & super sexy woman - she can't keep her hands off me - 1990 - dreamland.jpg"
First set the modification time so they are sortable by year.

#!/usr/bin/perl

# e. dziewa march 2007

use warnings;

my $pathname = "/absolute/path";
    print "$pathname set\n";

my $timereturn = time();
    print "$timereturn\n";

opendir (DIRHANDLE, $pathname) or die "couldn't open $pathname:\n $!";

my ( $filename, $modtime );
my $oneyear = 60 * 60 * 24 * 365;

while (defined($filename = readdir(DIRHANDLE))) {
    next if $filename =~ /^\.\.?$/;
    my $completename = "$pathname/$filename";
    (my $year_file_is_dated = $filename) =~ s/.*(\d{4}).*/$1/;
    $magic_number = ($year_file_is_dated - 1970) * $oneyear;
    ($acctime, $modtime) = (stat($completename))[8,9];
    utime($acctime, $magic_number, $completename) or die "couldn't change mtime $!\n";
}


Some files have over 10 people. I want stats of how many people, how many of each, the places, years, etc.


#!/usr/bin/perl

# e. dziewa april 2007

use strict;
use warnings;

my ($char_count, $word_count) = 0;
my ($filename, @who, @when, @what, $people, $when, $place);
my (@filename_array_split, $name, $year, $what);
my (%names_hash, %years_hash, %what_hash);
my @words;
my ($where, @where, %where_hash);
my $pathname = "/absolute/path";

my ($i, $j) = 0;

opendir (DIRHANDLE, $pathname) or die "couldn't open $pathname:\n$!";

while (defined($filename = readdir(DIRHANDLE))) {
    next if $filename =~ /^\.\.?$/;
    CHECK_FIELDS();
    ($people, $what, $when, $where) = split / - /, $filename;
    @who = split / \& /, $people;
    @when = $when;
    @what = split / /, $what;
    chop $where; chop $where; chop $where; chop $where;
    @where = $where;
    @filename_array_split = split //, $filename;
    $char_count += $#filename_array_split;
    @words = split /\b/, $filename;
    $word_count += $#words;

    foreach $name (@who) { $names_hash{$name}++ }
    foreach $year (@when) { $years_hash{$year}++ }
    foreach $what (@what) { $what_hash{$what}++ }
    foreach $place (@where) { $where_hash{$where}++ }
}

print "\n\nYears:\n\n";
foreach my $sorted_dates (sort keys %years_hash) {
    print "$sorted_dates -> $years_hash{$sorted_dates}";
     $i++;
    if ($i % 7) { print "\t" }
    else { print "\n" }
}

$i = 0;
my @k = sort { $names_hash{$b} <=> $names_hash{$a} } keys %names_hash;

printf "\n\n%d People:\n\n", ($#k +1);
foreach my $k (@k) {
    printf("%-15s-> %3d", $k, $names_hash{$k});
    $i++;
    if ($i % 4) { print "\t" }
    else { print "\n" }
}

$i = 0;
my @m = sort { $where_hash{$b} <=> $where_hash{$a} } keys %where_hash;

printf "\n\n%d Places:\n\n", ($#m +1);
foreach my $m (@m) {
    printf("%-30s-> %3d", $m, $where_hash{$m});
    $i++;
    if ($i % 3) { print "\t" }
    else { print "\n" }
}

$i = 0;
my @w = sort { $what_hash{$b} <=> $what_hash{$a} } keys %what_hash;

printf "\n\n%d \(Title\) words sorted by frequency:\n\n", ($#w +1);
foreach my $n (@w) {
    printf("%-14s-> %3d", $n, $what_hash{$n});
    $i++;
    if ($i % 5) { print "\t" }
    else { print "\n" }
}

$i = 0;
my @global = keys %what_hash;

printf "\n\n%d \(Title\) words sorted alphabetically:\n\n", ($#w +1);
my @new_w_sorted = sort { lc $a cmp lc $b } @global;
my $rows = ($#new_w_sorted / 5 + 1);
for (my $q = 0; $q <= $rows; $q++) {

    printf("%-14s-> %3d\t%-14s-> %3d\t%-14s-> %3d\t%-14s-> %3d\t%-14s-> %3d\n", $new_w_sorted[$q], $what_hash{$new_w_sorted[$q]}, $new_w_sorted[$q + $rows +1 ], $what_hash{$new_w_sorted[$q + $rows +1 ]}, $new_w_sorted[$q + $rows * 2 + 2 ], $what_hash{$new_w_sorted[$q + $rows * 2 + 2 ]}, $new_w_sorted[$q + $rows * 3 + 3 ], $what_hash{$new_w_sorted[$q + $rows *3 + 3 ]}, $new_w_sorted[$q + $rows * 4 + 4 ], $what_hash{$new_w_sorted[$q + $rows * 4 + 4 ]});
}

printf "\n\ntotal characters -> %d\tTotal Words -> %d", $char_count, $word_count;

sub CHECK_FIELDS { no warnings; $filename =~ /^(.* - ?)(.* - ?)(.* - ?).*$/ ? return $_ : print }


Here's the nice → output

In the next posts I will create thumbnail, and mid-size images. Enter the files into a database. and create a front end viewer for web browsers.

   
Comments
No comments.
Comments for this entry available via RSS.
Comment Area
Your Name
Your Email (will not be published)
Your Website
Your Comment
Profanity is Prohibited
eric.dziewa.com is running WordPress.
WhiteSpace theme designed by E. Dziewa.
All content © E. Dziewa.
Thanks for stopping by.