Skip to content

Instantly share code, notes, and snippets.

@evandhoffman
Created February 28, 2012 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evandhoffman/1934007 to your computer and use it in GitHub Desktop.
Save evandhoffman/1934007 to your computer and use it in GitHub Desktop.
Resize jpegs and rename them into date-based folders.
#!/usr/bin/perl
# Resize all images in a directory for the Insignia 7" Digital photo
# frame (Model: NS-DPF0712G), 800x480 resolution.
use strict;
use warnings;
use File::Find;
use Image::Magick;
use Image::ExifTool;
use File::Path qw(make_path);
use Time::ParseDate;
use POSIX qw(strftime);
my $source_dir = '/photos/';
my $target_dir = '/resized-photos/1024x1024/';
my $resize_width = 1024;
my $resize_height = 1024;
my $scale_dimensions = $resize_width . 'x' . ($resize_height + 200);
my $crop_dimensions = $resize_width . 'x' . $resize_height;
my $crop_gravity = 'Center';
my ($image, $x);
my $convert_count = 0;
my $start = time;
find(\&wanted, $source_dir);
my $end = time;
my $elapsed = $end - $start;
my $speed = $convert_count / $elapsed;
print "Total converted files: $convert_count, elapsed time: $elapsed, speed: $speed/second\n";
sub wanted {
my ($dev,$ino,$mode,$nlink,$uid,$gid);
my $image = Image::Magick->new;
$image->Set(gravity=>$crop_gravity);
my $exifTool = new Image::ExifTool;
$exifTool->Options(DateFormat=>'%Y-%m-%d %H:%M:%S');
if ( (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
/^.*jpg\z/si) {
# Have perl open the file so exiftool & imagemagick can read it without opening it twice
open FILE, $_ or die $!;
print "Opening file $_\n";
my $info = $exifTool->ImageInfo(\*FILE);
seek (FILE, 0, 0);
$x = $image->Read(file=>\*FILE);
warn "$x" if "$x";
$x = $image->Scale(geometry=>$scale_dimensions);
warn "$x" if "$x";
my $width = $image->Get('width');
if ($width >= $resize_width) {
# Don't crop images that are in portrait orientation.
#$x = $image->Crop(geometry=>$crop_dimensions);
warn "$x" if "$x";
}
#foreach (keys %$info) {
# print "\t$_ => $$info{$_}\n";
#}
my $create_epoch = parsedate($$info{'CreateDate'});
my @create_time = localtime($create_epoch);
my $date_path = $target_dir . '/'. strftime('%Y/%Y-%m/%Y-%m-%d/',@create_time);
my $date_file = strftime('%Y/%Y-%m/%Y-%m-%d/%Y%m%d-%H%M%S.jpg',@create_time);
print "dp: $date_path\n";
my $output_filename;
if (defined($create_epoch)) {
$output_filename = $target_dir . '/'. $date_file;
make_path($date_path) unless -d $date_path;
} else {
$output_filename = $target_dir . '/'.$_;
}
print "Writing file $output_filename\n";
$x = $image->Write(filename=>$output_filename, quality=>80);
warn "$x" if "$x";
close( FILE );
$convert_count++;
my $elapsed = time - $start;
my $speed = $elapsed > 0 ? $convert_count / $elapsed : 0;
print "converted $convert_count: $_\n";
if ($convert_count % 25 == 0) {
print "Rate so far: $speed/second\n";
}
} else {
print "Skipping $_\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment