#! /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 "\n"; print PHOTOALBUMPAGE "Web Photo Album: $dirname\n"; print PHOTOALBUMPAGE "\n"; print PHOTOALBUMPAGE "\n"; print PHOTOALBUMPAGE "\n"; print PHOTOALBUMPAGE "
\n"; print PHOTOALBUMPAGE "

\n"; print PHOTOALBUMPAGE "Web Photo Album: $dirname\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 "\n"; print MASTERPAGE "Web Photo Album: $dirname\n"; print MASTERPAGE "\n"; # start writing the slideshow page open(SLIDESHOWPAGE, '>index_slideshow.html'); print SLIDESHOWPAGE "\n"; print SLIDESHOWPAGE "\n"; print SLIDESHOWPAGE "\n"; print SLIDESHOWPAGE "Web Slide Show: $dirname\n"; print SLIDESHOWPAGE "\n"; print SLIDESHOWPAGE "\n"; print SLIDESHOWPAGE "\n"; print SLIDESHOWPAGE "
\n"; print SLIDESHOWPAGE "

\n"; print SLIDESHOWPAGE "Web Slide Show: $dirname\n"; print SLIDESHOWPAGE "

\n"; print SLIDESHOWPAGE "

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 "\n"; print MASTERPAGE "Web Photo Album: $dirname\n"; print MASTERPAGE "\n"; # java stuff for the JAVA page print MASTERPAGE "\n"; print MASTERPAGE "\n"; print MASTERPAGE "\n"; print MASTERPAGE "
\n"; print MASTERPAGE "

\n"; print MASTERPAGE "Web Photo Album: $dirname\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.
\n"; print MASTERPAGE "Click on a thumbnail to open the fullsize image.
\n"; print MASTERPAGE "When the larger image opens, click on it or back on the index page to close the larger image.

\n"; # use a count for determining the number of columns of thumbnails $count = 0; # use a count of images $next = 0; # create a page for each image $filenum = 0; foreach $file (@filez) { $filenum++; print "Indexing $filenum of $numfiles...\n"; # get the previous and next files $next = $next + 1; $prev = $next - 2; $prevfile = "$filez[$prev].html"; if ($next < $numfiles) { $nextfile = "$filez[$next].html"; } ($base, $path, $type) = fileparse("$file", qr{\..*}); # is this a regular image? If so, make its own page if ( ( lc($type) eq ".jpg" ) or ( lc($type) eq ".jpeg" ) or ( lc($type) eq ".gif" ) ) { # write the individual photo album image page open(IMAGEPAGE, ">pages/$file.html"); print IMAGEPAGE "\n"; print IMAGEPAGE "\n"; print IMAGEPAGE "\n"; print IMAGEPAGE "$file\n"; print IMAGEPAGE "\n"; # write the individual slide show image page open(SLIDEPAGE, ">slideshow_pages/$file.html"); print SLIDEPAGE "\n"; print SLIDEPAGE "\n"; if ($next < $numfiles ) { print SLIDEPAGE "\n"; } print SLIDEPAGE "\n"; print SLIDEPAGE "$file\n"; print SLIDEPAGE "\n"; # there might be a sound associated with this file @base = split ( /\./, "$file" ); $b = $base[0]; $wav = "$b.wav"; if ( -e $wav ) { print SLIDEPAGE "\n"; print IMAGEPAGE "\n"; print "WAV $wav file linked\n"; } print IMAGEPAGE "\n"; print IMAGEPAGE "\n"; print IMAGEPAGE "
\n"; print IMAGEPAGE "

\n"; print IMAGEPAGE "$file ($filenum of $numfiles)
\n"; print IMAGEPAGE "

\n"; print SLIDEPAGE "\n"; print SLIDEPAGE "\n"; print SLIDEPAGE "
\n"; print SLIDEPAGE "

\n"; print SLIDEPAGE "$file ($filenum of $numfiles)
\n"; print SLIDEPAGE "

\n"; print SLIDEPAGE "Slides change every 5 seconds.
\n"; # navigation print IMAGEPAGE "Index\n"; if ($next != 1) { print IMAGEPAGE "Previous\n"; } if ($next < $numfiles) { print IMAGEPAGE "Next\n"; } print IMAGEPAGE "
\n"; print IMAGEPAGE "
\n"; print SLIDEPAGE "Index\n"; print SLIDEPAGE "
\n"; print SLIDEPAGE "
\n"; # the image # if a video file and an image file exist, make sure the link points to the video #if ( ( -e "$base.avi" ) or ( -e "$base.AVI" ) or ( -e "$base.WMV" ) or ( -e "$base.wmv" ) ) { if ( ( -e "$base.avi" ) or ( -e "$base.AVI" ) ) { #@vidfile = `ls $base.[Aa][Vv][Ii] $base.[Ww][Mm][Vv]`; @vidfile = `ls $base.avi`; $vid = $vidfile[0]; chomp $vid; print IMAGEPAGE "This is a video. Click to Play.
\n"; print IMAGEPAGE "
\n"; print SLIDEPAGE "
\n"; } else { print IMAGEPAGE "
\n"; print SLIDEPAGE "
\n"; } print IMAGEPAGE "
"; print IMAGEPAGE "
\n"; print IMAGEPAGE "
\n"; print IMAGEPAGE "Generated by \n"; print IMAGEPAGE "webphoto.pl\n"; print IMAGEPAGE "
\n"; print IMAGEPAGE "\n"; print IMAGEPAGE "\n"; print SLIDEPAGE "
"; print SLIDEPAGE "
\n"; print SLIDEPAGE "
\n"; print SLIDEPAGE "Generated by \n"; print SLIDEPAGE "webphoto.pl\n"; print SLIDEPAGE "
\n"; print SLIDEPAGE "\n"; print SLIDEPAGE "\n"; # the URL of the individual page $url0 = " "; # make a border if there is an AVI $url1 = " \n"; if ( -e "$base.avi" ) { $url1 = " \n"; } $url_photoalbum = "$url0\n $url1\n"; # URL for slideshow page $url0 = " "; $url_slideshow = "$url0\n $url1\n"; # if a video file and an image file exist, make sure the link points to the video #if ( ( -e "$base.avi" ) or ( -e "$base.AVI" ) or ( -e "$base.WMV" ) or ( -e "$base.wmv" ) ) { if ( ( -e "$base.avi" ) or ( -e "$base.AVI" ) ) { #@vidfile = `ls $base.avi $base.[Ww][Mm][Vv]`; @vidfile = `ls $base.avi`; $vid = $vidfile[0]; chomp $vid; print MASTERPAGE "  \n"; } else { ($soundfile = $file) =~ s|jpg|wav| ; $url_java0 = ""; $url_java1 = "  \; \n"; $url_java = "$url_java0\n $url_java1\n"; print MASTERPAGE "$url_java"; } print PHOTOALBUMPAGE "$url_photoalbum"; print SLIDESHOWPAGE "$url_slideshow"; close IMAGEPAGE; } } # end the HTML print PHOTOALBUMPAGE "\n\n\n"; print PHOTOALBUMPAGE "


\n"; print PHOTOALBUMPAGE "
\n"; print PHOTOALBUMPAGE "Generated by \n"; print PHOTOALBUMPAGE "webphoto.pl\n"; print PHOTOALBUMPAGE "
\n"; print PHOTOALBUMPAGE ""; print PHOTOALBUMPAGE ""; close PHOTOALBUMPAGE; print SLIDESHOWPAGE "\n\n\n"; print SLIDESHOWPAGE "


\n"; print SLIDESHOWPAGE "
\n"; print SLIDESHOWPAGE "Generated by \n"; print SLIDESHOWPAGE "webphoto.pl\n"; print SLIDESHOWPAGE "
\n"; print SLIDESHOWPAGE ""; print SLIDESHOWPAGE ""; close SLIDESHOWPAGE; print MASTERPAGE "


\n"; print MASTERPAGE "
\n"; print MASTERPAGE "Generated by \n"; print MASTERPAGE "webphoto.pl\n"; print MASTERPAGE "
\n"; print MASTERPAGE ""; print MASTERPAGE ""; close MASTERPAGE; # error message sub error { print "\nIt appears a webphoto album or other index.html already exists in this directory.\n"; print "If you want to overwrite, run with the -o option.\n\n"; }