#!/usr/bin/perl # exif2tms # Extract EXIF thumbs and store them according to Thumbnail Managing Standard. # More info: http://jens.triq.net/thumbnail-spec/ # Requirements: sng, imagemagick, exif-get-rotation use strict; use warnings; use Cwd qw(abs_path); use Digest::MD5 qw(md5_hex); use URI::Escape; my $thumbdir = "$ENV{HOME}/.thumbnails/normal"; for my $file (@ARGV) { print STDERR "*** Processing file: '$file'\n"; # make sure $file is an absolute path $file = abs_path($file); # create escaped filename # the code aims to be fully compatible with GLib escape function (great joke isn't it? :D) my $escaped = uri_escape("file://$file", '^-!\\$&\'()*+,./0-9:=\\@A-Z_a-z~'); # generate md5 hash my $hash = md5_hex($escaped); # call exif to extract the thumbnail file !system ('exif', '-o', "$thumbdir/$hash.jpg", '-e', $file) or die "EXIF thumbnail extraction failed!"; # get mtime :) my $mtime = (stat $file)[9]; # retrieve EXIF orientation my $safefile = $file; $safefile =~ s!'!'\\''!g; my $angle = `exif-get-rotation '$safefile'`; print STDERR "Using EXIF rotation value of $angle\n"; # convert the resulting file to PNG !system ('convert', "$thumbdir/$hash.jpg", qw(-resize 128x128 -rotate), $angle, qw(-quality 100), "$thumbdir/$hash-1.png") or die "convert failed"; unlink "$thumbdir/$hash.jpg"; # generate SNG !system ('sng', "$thumbdir/$hash-1.png") or die "png->sng failed"; # add tEXt fields open my $sng, '>>', "$thumbdir/$hash-1.sng" or die "cannot open sng: $!"; print $sng <png failed"; unlink "$thumbdir/$hash-1.sng"; # give the resulting file a proper name rename "$thumbdir/$hash-1.png", "$thumbdir/$hash.png"; }