#!/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 # # Gerald Oskoboiny, 27 Nov 2001 # # source: http://impressive.net/software/photo/source/make_variant # # $Id: make_variant,v 1.11 2006/10/10 04:16:24 gerald Exp $ # use strict; my $height = shift; # arg 1 my $suffix = "-" . shift; # arg 2 my $identify = "identify"; my $convert = "convert"; my $convert_opts = " -dither"; my $verbose = 0; 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 $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} "; } system "$convert $shave_opts $rotate_opts +repage -resize x$height $convert_opts $file $variant_name"; system "jhead -dt $variant_name | grep -v 'bytes removed\$' | grep -v '^Modified: '"; print "Done $variant_name\n" if $verbose; } exit; # # changelog: # # $Log: make_variant,v $ # Revision 1.11 2006/10/10 04:16:24 gerald # bug in logic of ignoring orientation from med files # # Revision 1.10 2006/10/09 04:42:51 gerald # added +repage and removed -size since no worky with more modern ImageMagick # distributed with ubuntu dapper; also a couple changes to deal with # make_variants now generating smaller variants based on foo-med.jpg # # Revision 1.9 2006/01/09 06:53:51 gerald # avoid generating variants of variants # # Revision 1.8 2005/12/09 21:41:17 gerald # added special handling for square-cropped images (those with suffix 'sq') # # Revision 1.7 2005/01/27 20:58:57 gerald # compensate for change in jhead's output # # Revision 1.6 2004/12/03 14:57:00 gerald # added a pointer to block_until_load_less_than source # # Revision 1.5 2003/06/16 05:19:35 gerald # (hopefully) speed things up by specifying -size before -resize; see # http://impressive.net/archives/fogo/20030613222634.GC29059@impressive.net # # Revision 1.4 2002/08/28 05:56:30 gerald # added load checker # # Revision 1.3 2002/08/08 03:58:03 gerald # added a pointer to the source url # # Revision 1.2 2002/04/29 04:55:22 gerald # added changelog #