Professional Documents
Culture Documents
Jon Allen
http://perl.jonallen.info - jj@jonallen.info
Organisation
• Digital photography can create lots of files
– Camera filenames are usually meaningless
• See http://search.cpan.org/dist/Image-ExifTool
Photo Tools – Perl scripts for digital photography
perl.jonallen.info
Getting the timestamp
• We need the timestamp as a Time::Piece object
– Time::Piece is a core module from Perl 5.10.0
– See http://search.cpan.org/dist/Time-Piece
sub exif_timestamp {
my $filename = shift;
my $format = '%Y:%m:%d:%H:%M:%S';
my $exif = Image::ExifTool->new();
my $tags = $exif->ImageInfo($filename,
{DateFormat=>$format});
return Time::Piece->strptime($tags->{DateTimeOriginal},
$format);
}
my $time = exif_timestamp($filename);
my $offset = -8; # 8 hours behind UK
my $template = 'YYYY/MM/DD/YYYY-MM-DD_HH24-MI-SS';
my $target = $time->strftime(oracle2posix($template));
my %find_options;
$find_options{no_chdir} = 1;
$find_options{wanted} = sub {
if (/\.(jpg|jpe|jpeg|tif|tiff)$/i) {
copy_rename(
source => $File::Find::name,
destination => $destination_dir,
template => $template,
offset => $offset,
);
}
};
find(\%find_options, $source_directory);
$exif->SetNewValuesFromFile($filename);
$exif->SetNewValue('Keywords',$keywords);
$exif->SetNewValue('XPKeywords',join ', ',@$keywords);
$exif->WriteInfo($filename);
• 3:2 (1:1.5)
– Based on 35mm film format
– Canon, Nikon, Pentax, Sony DSLRs
• 4:3 (1:1.33)
– Olympus, Panasonic, Leica DSLRs
– Most compact digital cameras
3:2 format:
7% lost (½")
4:3 format:
7x5
5% lost (¼")
7x5 paper
4:3 image
No cropping!
my $paper_size = 'A4';
Paper::Specs->units('in');
my $page = Paper::Specs->find(code=>$paper_size)
or die("Invalid paper size\n");
my $width = $page->sheet_width;
my $height = $page->sheet_height;
my $paper_longside = max($paper_width,$paper_height);
my $paper_shortside = min($paper_width,$paper_height);
my $paper_aspect = $paper_longside / $paper_shortside;
my $image_longside = max($image_width,$image_height);
my $image_shortside = min($image_width,$image_height);
my $image_aspect = $image_longside / $image_shortside;
• If $image_aspect >
$paper_aspect, we need to
add borders to the short
side of the image
• Disadvantages
– Fewer facilities than Image::Magick
– Strips image metadata (EXIF)
my $source = Imager->new->read(file=>$source_filename);
my $dest = Imager->new(
xsize => $source->getwidth() + 2 * $border_width,
ysize => $source->getheight() + 2 * $border_height,
bits => $source->bits,
channels => $source->getchannels
);
$dest->box(filled=>1, color=>'white');
$dest->paste(img=>$source,
top=>$border_height, left=>$border_width);
$dest->write(file=>$target_filename,jpegquality=>100);
$exif->SetNewValuesFromFile($source_filename);
$exif->WriteInfo($target_filename);
Any questions?
http://perl.jonallen.info/talks