Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Organize a huge number of photos into a hierarchy' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Organize a huge number of photos into a hierarchy
Authored by: rotaiv on Jan 06, '05 04:59:54PM
I liked the idea of using jhead but my needs were a little different. I wanted my target directory structure like this: /Photos/2004/01/06. Also, I did not want to change the image name. Finally, I wanted to use jhead's "-ft" to change the file time to the EXIF capture date/time.

I got the idea after looking at DateTree. This is a really nice application and will do almost what I wanted. The only difference is that I wanted the files "moved" and not "copied".

Since the files are not renamed, there is the possibility of a duplicate so I had to factor that in as well. I've written a few perl scripts before so I decided to write one to satsify my needs. It's fairly basic but does the job :)


#!/usr/bin/perl

use File::Path;
use File::Basename;

$basedir = "~/Photos";

# Process each file
foreach $fpath (@ARGV) { &main; }

sub main {

  # Skip invalid files
  if(! -e $fpath) {
    print "$fpath -> [invalid]\n";
    next;
  }

  # Read EXIF data from image
  @exifdata = `jhead -exonly $fpath`;

  # Skip if no EXIF data found
  if ($#exifdata == 0) {
    print "$fpath -> [skipped]\n";
    next;
  }

  # Process each EXIF field
  foreach (@exifdata) {

    # Look for "Date/Time" field
    if ( ($imgyr, $imgmo, $imgdy) = /Date\/Time.*\s:.(\d+):(\d+):(\d+)\s./ ) {

      $imgdir="$imgyr/$imgmo/$imgdy";

      $fname = File::Basename::basename($fpath);

      # Check if duplicate file exist in target directory
      if(-e "$basedir/$imgdir/$fname") {
        print "$fpath -> [duplicate] $basedir/$imgdir/$fname\n";
        next;
      }

      # Make directories if not present
      mkpath("$basedir/$imgdir") unless -e "$basedir/$imgdir";
      # Change file date/time to match EXIF
      system("jhead -ft $fpath > /dev/null");
      print "$fpath -> $basedir/$imgdir/$fname\n";
      # Move file to target directory
      rename($fpath,"$basedir/$imgdir/$fname");

    }

  }

}
As the original post suggested, you can use "find" to search for the files you need and execute this script:

find ~/NewPhotos -type f -name *.JPG -exec mvimg.pl {} \; | tee mvimg.log
The "| tee mvimg.log" at the end creates a log file of the output so you can grep for "skipped" or "duplicate" messages.

I'll probably just use it like mvimg.pl *.JPG from the directory containing the photos I want to organize.

Don't forget to put this script in your path somewhere and chmod u+x mvimg.pl so it is executable.

[ Reply to This | # ]

Organize a huge number of photos into a hierarchy
Authored by: rotaiv on Jan 06, '05 05:02:10PM

I forgot to mention that I used to use iPhoto but for various reasons, I switched to iView Media. My intention now is to download my new images, run my <tt>mvimg.pl</tt> script then add them to my iView Media catalogs.



[ Reply to This | # ]