#!/usr/bin/perl -w # # make_variant: create a resized variant of each image named in @ARGV # # usage e.g.: # make_variant 128 tn *.jpg # make_variant 300 sm *.jpg # make_variant 480 med *.jpg # # WARNING: this script prunes embedded metadata from the variants it # generates, to reduce image sizes. It also has a bunch of stuff hardcoded # that you will want to change before using it, e.g. my name and URIs. # # Gerald Oskoboiny, 27 Nov 2001 # # source: http://impressive.net/software/photo/source/make_variant # # $Id: make_variant,v 1.13 2009-06-10 02:12:19 gerald Exp $ # use strict; my $height = shift; # arg 1 my $suffix = "-" . shift; # arg 2 my $identify = "identify"; my $convert = "convert"; my $quality_opts = " -quality 75"; my $verbose = 0; my $exiftool = '/usr/bin/exiftool'; ( -x $exiftool ) or die "$exiftool does not exist or is not executable"; $exiftool .= " -q -overwrite_original "; foreach my $file (@ARGV) { # remaining args if ( ! -r $file ) { print STDERR "Can't read $file! skipping it...\n"; next; } my $variant_name = $file; $variant_name =~ s/-(sq|tn|sm|med)\.jpg$/.jpg/; # avoid variants of variants my $uri = "http://impressive.net/photos"; if ( $ENV{PWD} =~ m,^/home/gerald/www/people/gerald/(.*), ) { $uri = "http://impressive.net/people/gerald/" . $1 . "/". $variant_name; 1 while $uri =~ s,[^\.^/]+/\.\./,,; $uri =~ s/\.jpg$/-sm.html/; } $variant_name =~ s/\.jpg/${suffix}.jpg/; if ( -f $variant_name ) { print STDERR "Variant $variant_name already exists! skipping it...\n" if $verbose; next; } my $rotate_opts = ""; if ( $file !~ /-med.jpg$/ ) { # ignore bogus orientation in med files chomp( my $orientation = `epinfo -T Orientation $file`); if ( $orientation == 6 ) { $rotate_opts .= " -rotate 90"; } elsif ( $orientation == 8 ) { $rotate_opts .= " -rotate 270"; } } my $shave_opts = ""; if ( $suffix eq "-sq" ) { # special handling for square-cropped images my ($width,$height) = split('x',`$identify $file | cut -d" " -f3`); chomp($height); my $shave_x = ($width - $height) / 2; $shave_x = 0 if $shave_x < 0; my $shave_y = ($height - $width) / 2; $shave_y = 0 if $shave_y < 0; $shave_opts = " -shave ${shave_x}x${shave_y} "; } # @@ add support for varying gravity for sq regions # # command line kludge to regenerate sq.jpg's with top gravity: # rm the sq.jpg's you want top-gravitied, then run: # for med in *med.jpg ; do sq=`echo $med | sed 's/med/sq/'`; [ -f $sq ] && continue; convert -crop 320x320+0+0 $med temp; convert -geometry 100x100 temp $sq ; rm temp; jhead -dt $sq; done if ( $suffix =~ "-(sq|tn)" ) { # special handling for smaller variants $quality_opts = " -quality 85"; } system "$convert $shave_opts $quality_opts $rotate_opts +repage -resize x$height $file $variant_name"; if ( $suffix !~ "-med" ) { # delete thumbnail and all metadata except dates from smaller variants system "$exiftool -all= -AllDates'>Alldates' $variant_name"; } # set the copyright field system "$exiftool -d %Y -copyright'