#! /usr/bin/perl #----------------------------------------------------------- # creates a web photo album from all *.jpg and *avi files in current dir # only runs on Win32 now # creates a directory called pages containing the pages for each individual image # creates a file called index.html (the navigation page) # creates a dir called fullsize containing the images # creates a dir called thumbnails containing thumbnail images # requires a few utilities: # perl (www.cpan.org) is the programming language # ls.exe (gnu utility for listing files) # mv.exe (gnu utility for moving [i.e., renaming] files) # rm.exe (gnu utility for deleting files) # cp.exe (gnu utility for copying files) # get these at http://gis.washington.edu/phurvitz/utils/unix/ # 1. nconvert (to make the rescaled images) # http://www.xnview.com/ # 2. virtualdub (to make thumbs of the avi files, also does rotate & other effects) # http://www.virtualdub.org # only handles avi files, not mov or wmv. # # all utilites need to be in the PATH so they can be run as commands with no pathnames # otherwise you will need to alter the script # # another useful piece of software is bink and smacker: # http://www.radgametools.com (convert formats) #----------------------------------------------------------- #OS if ( "$^O" ne "MSWin32" ) { print "OS is not $^O"; exit; } # to make getting the basename of the dir easy use File::Basename; use File::Copy; use File::Stat; use Cwd; $command = basename ($0); # args? if ($#ARGV == -1) { print "Usage $command -on srcdir\n"; print " (-o = overwrite; -n = no overwrite)\n"; exit; } # source dir? if ($#ARGV == 1) { if (-d $ARGV[1]) { $srcdir = "$ARGV[1]/"; } } else { $srcdir = ""; } # convert backslashes to forward slashes $srcdir =~ s|\/\/|\/|; # overwrite without complaining? if ($ARGV[0] eq "-o") { if (! -e "thumbnails") { system "mkdir thumbnails"; } if (! -e "fullsize") { system "mkdir fullsize"; } } elsif ($ARGV[0] eq "-n") { # check the index.html page if (-e "index.html") { print "\nindex.html already exists. \n"; &error; exit; } # check the thumbnails directory if (-e "thumbnails") { print "\nthumbnails directory already exists. \n"; &error; exit; } else { system "mkdir thumbnails"; } # check the fullsize directory if (-e "fullsize") { print "\nfullsize directory already exists. \n"; &error; exit; } else { system "mkdir fullsize"; } } else { print "Usage webphoto.pl -o|-n\n"; print " (-o = overwrite; -n = no overwrite)\n"; exit; } # lowercase all files @allfiles = `ls *.*`; foreach $f ( @allfiles ) { chomp $f; $lcfile = lc ( $f ); if ( $f ne $lcfile ) { system "mv \"$f\" \"$lcfile\""; } } # delete spaces from filenames foreach $f ( @allfiles ) { chomp $f; ($nsfile = $f) =~ s/ //g; if ( $f ne $nsfile ) { system "mv \"$f\" $nsfile"; } } # convert video files using virtualdub #@vidfiles = `ls $srcdir*.[Aa][Vv][Ii] $srcdir*.[Ww][Mm][Vv]`; @vidfiles = `ls $srcdir*.avi`; foreach $vidfile ( @vidfiles ) { # get the basename of the image file chomp $vidfile; ($base, $path, $type) = fileparse("$vidfile", qr{\..*}); $basefile = "$base$type"; $srcfile = "$srcdir$basefile"; $srcfile =~ s|\/|\\|g; $cwd = getcwd(); $cwd =~ s|\/|\\\\|g; $vidfilefullpath = "$cwd\\\\$base$type\n"; chomp $vidfilefullpath; $vidfilecappath = "$cwd\\\\$base\n"; chomp $vidfilecappath; $b0 = join ( "", "$base", "0" ); $vidfilecapout0 = "$cwd\\\\$b0.jpg\n"; chomp $vidfilecapout0; $vidfilecapout = "$cwd\\\\$base.jpg\n"; chomp $vidfilecapout; if ( ! -e $vidfilecapout ) { open (VDFILE, '>vdfile.vcf'); print VDFILE "VirtualDub.Open(\"$vidfilefullpath\",0,0);\n"; print VDFILE "VirtualDub.audio.SetSource(1);\n"; print VDFILE "VirtualDub.audio.SetMode(0);\n"; print VDFILE "VirtualDub.audio.SetInterleave(1,500,1,0,0);\n"; print VDFILE "VirtualDub.audio.SetClipMode(1,1);"; print VDFILE "VirtualDub.audio.SetConversion(0,0,0,0,0);\n"; print VDFILE "VirtualDub.audio.SetVolume();\n"; print VDFILE "VirtualDub.audio.SetCompression();\n"; print VDFILE "VirtualDub.audio.EnableFilterGraph(0);\n"; print VDFILE "VirtualDub.video.SetInputFormat(0);\n"; print VDFILE "VirtualDub.video.SetOutputFormat(7);\n"; print VDFILE "VirtualDub.video.SetMode(3);\n"; print VDFILE "VirtualDub.video.SetFrameRate(0,1);\n"; print VDFILE "VirtualDub.video.SetIVTC(0,0,-1,0);\n"; print VDFILE "VirtualDub.video.SetCompression();\n"; print VDFILE "VirtualDub.video.filters.Clear();\n"; print VDFILE "VirtualDub.audio.filters.Clear();\n"; print VDFILE "VirtualDub.subset.Clear();\n"; print VDFILE "VirtualDub.subset.AddRange(0,1);\n"; print VDFILE "VirtualDub.project.ClearTextInfo();\n"; print VDFILE "VirtualDub.SaveImageSequence(\"$vidfilecappath\", \".jpg\", 0, 2, 95);\n"; print VDFILE "VirtualDub.audio.SetSource(1);\n"; print VDFILE "VirtualDub.Close();\n"; close VDFILE; system ( "virtualdub /s vdfile.vcf /x" ); system ( "mv $vidfilecapout0 $vidfilecapout" ); system ( "rm vdfile.vcf" ) } } # foreach $vidfile # list of jpeg and gif files in current dir @picfiles = `ls $srcdir*.jpg $srcdir*.jpeg $srcdir*.gif`; #@picfiles = `ls $srcdir*.[Jj][Pp][Gg] $srcdir*.[Aa][Vv][Ii] $srcdir*.[Ww][Mm][Vv]`; $numfiles = @picfiles; # copy the perl file to this directory copy ( "$0", "webphoto.pl.txt" ); # create thumbnails $imgcount = 0; foreach $imgfile (@picfiles) { # how far have we gotten? $imgcount++; # get the basename of the image file chomp $imgfile; ($base, $path, $type) = fileparse("$imgfile", qr{\..*}); $basefile = "$base$type"; $srcfile = "$srcdir/$basefile"; # thumbnail exist? if ( -e "thumbnails/$basefile" ) { next; } print "Thumbnailing image $imgcount of $numfiles...\n"; # does the image file have any content? $size = (stat($imgfile))[7]; if ( $size == 0 ) { next; } # is this a jpeg if ( ( lc($type) eq ".jpg" ) or ( lc($type) eq ".jpeg" ) or ( lc($type) eq ".gif" ) ) { # try to nconvert just with the -info option to get file properties @info = `nconvert -quiet -info $srcfile`; # did I find the word "Failed"? means the image has problems @match = grep(/.*Failed.*/, @info ); $count = @match; if ($count == 1) { print "Error in image file $srcfile, or is not an image.\n"; next; } # does the thumbnail exist? if ( ! -e "thumbnails/$basefile" ) { # get the width & height of the image for resizing @info = `nconvert -quiet -info $imgfile`; $width = "$info[5]"; $width =~ s/ //g; @wid1 = split (":",$width); $wid = $wid1[1]; chop $wid; $height = "$info[5]"; $height =~ s/ //g; @hgt1 = split (":",$height); $hgt = $hgt1[1]; chop $hgt; #@height = split (/ */, $info[5]); #$ht = ("$height[3]"); #chop $ht; # I want the maximum thumbnail dimension to be 150 pixels if ($wid > $ht) { $thumbscale = 150 / $wid * 100; $maxdim = $wid; } else { $thumbscale = 150 / $ht * 100; $maxdim = $ht; } # rescale to create the thumbnail at a max if 150 pixels print "\nmaking thumbnail ($srcfile => thumbnails/$basefile)...\n"; system "nconvert -quiet -resize $thumbscale% $thumbscale% -o thumbnails/$basefile $srcfile"; # is the original image larger than 800 pixels wide or high? If so, rescale if ($maxdim > 800) { if ($wid > $ht) { print "making rescaled image ($srcfile => fullsize/$basefile)... \n"; $fullscale = 800 / $wid * 100; system "nconvert -quiet -resize $fullscale% $fullscale% -o fullsize/$basefile $srcfile"; } elsif ($ht > $wid) { print "making rescaled image ($srcfile => fullsize/$basefile)... \n"; $fullscale = 800 / $ht * 100; system "nconvert -quiet -resize $fullscale% $fullscale% -o fullsize/$basefile $srcfile"; } } # if rescaling not needed, just copy the file else { print "copying image ... \n"; system "cp $basefile fullsize"; } } # if thumbnails don't exist } # if jpg } # for each image file # mangle the list of files @filez = (); foreach $file (@picfiles) { chomp $file; ($base, $path, $type) = fileparse("$file", qr{\..*}); $filename = "$base$type"; push @filez, $filename; } # get directory name $pwd = getcwd; $dirname = basename( $pwd ); chomp $dirname; # make a "pages" dir for individual pages if (! -e "pages") { system("mkdir pages"); } # make a "slideshow_pages" dir for individual pages if (! -e "slideshow_pages") { system("mkdir slideshow_pages"); } # start writing the index page open(PHOTOALBUMPAGE, '>index_photoalbum.html'); print PHOTOALBUMPAGE "\n"; print PHOTOALBUMPAGE "
\n"; print PHOTOALBUMPAGE "Jump to Java Version | "; print PHOTOALBUMPAGE "Jump to Slide Show Version | Up
\n"; print PHOTOALBUMPAGE "Click on a thumbnail to start the photo album.
\n"; open(MASTERPAGE, '>index.html'); print MASTERPAGE "\n"; print MASTERPAGE "\n"; print MASTERPAGE "Jump to Photo Album Version | "; print SLIDESHOWPAGE "Jump to Java Version | Up
\n"; print SLIDESHOWPAGE "Click on a thumbnail to start the slide show. Slides advance every 5 seconds.
\n"; open(MASTERPAGE, '>index.html'); print MASTERPAGE "\n"; print MASTERPAGE "\n"; print MASTERPAGE "Jump to Photo Album Version | "; print MASTERPAGE "Jump to Slide Show Version | Up
\n"; print MASTERPAGE "Videos have a border around the image.