D&C Lug - Home Page
Devon & Cornwall Linux Users' Group

[ Date Index ][ Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] batch mode jpeg conversion ?



I've continued to refine the script for generating HTML index pages for
jpegs. You can now put some text next to the thumbnails (taken from a
file called "captions"), create different index pages and so on.

I'll set up a demo on the web soon, but for now here's the latest code 

Keith

------------------------------------------------------------------------

#!/usr/bin/tclsh

#Usage : mkpixndx [ subdir [ Page Title ... ] ]

#This script looks for *.jpeg, *.jpg, *.JPEG etc. files in the subdirectory 
#"subdir" (default "pictures"), generates largish thumbnails in 
#subdir/thumbnails using ImageMagick and writes an html catalogue page 
#"subdir.html" with the title and heading "Page Title" (default "Picture 
#Catalogue").

#Adding or removing jpegs to the subdirectory and re-running the script
#will add or delete entries in the catalogue. Replacing a jpeg by a later
#version with the same file name will cause the thumbnail to be updated. 

#To add captions to the thumbnails place a text file named "captions"
#in the directory with the jpegs. Entries should be of the form :
# filename.jpg
# Bold Heading
# Body of caption (several lines if necessary)
#Separate entries by one or more blank lines. "\n" is translated to the
#HTML <br> tag. Other tags may be included with caution.
#A special entry of the form :
# TITLE
# Title for the Picture Index Page
#can be placed anywhere in the captions file and will override the title
#(if any) given on the command line.

#Remember to do "chmod 755 mkpixndx"
#If the script is in the current directory you may need to call it with
# "./mkpixndx ..."

#NB you will need to hack the first two lines of code below to insert your 
#own email and web addresses.

# ---------------------------------------------------------------------------

#SUBSTITUTE YOUR WEBSITE ADDRESS HERE :
set url index.html

#SUBSTITUTE YOU EMAIL ADDRESS HERE :
set email "pix@xxxxxxxxxxxxxxxxxxxxxx"

#Take the first word of the command tail as the html filename and
#pictury directory name, or use "pictures" as the default
set filename [lindex $argv 0]
if {![string length $filename]} {set filename pictures}

#Are there any pictures?
if {![file isdirectory $filename]} {
   puts "Wot No Pictures!"
   exit
}
if {![regexp -nocase .*\.jp.*g [exec ls $filename]]} {
   puts "Wot No Pictures!"
   exit
}

#Get captions (if file present)
if [file exists $filename/captions] {
   set CAP [open $filename/captions]
   while {![eof $CAP]} {
      gets $CAP line
      if {![string match {} $line]} {
         set pic $line
         gets $CAP line
         set head($pic) $line
         set text($pic) {}
         while {![string match {} $line]} {
            gets $CAP line
            regsub -all {\\\n} $line "<br>" line
	    set text($pic) "$text($pic) $line"
         }
      }
   }
   close $CAP
}
if [array exists head] {set capslist [array names head]} else {set capslist {}}

#Make directory for thumbnails if necessary
if {![file isdirectory $filename/thumbnails]} {exec mkdir $filename/thumbnails}

#Go through thumbnails directory and delete any that are redundant
cd $filename/thumbnails
foreach thumb [glob -nocomplain th_*.jp*g] {
      if {[lsearch [exec ls ..] [string range $thumb 3 end]] == -1} {
      puts "Removing $thumb"; exec rm $thumb
   }
}
cd ../..

#Open file for writing (if it already exists it will be trashed!)
set HTML [open $filename.html w]

#Write out the html header (remember to escape all the quotes)
puts $HTML "<!-- X-URL: http://$url/$filename.html -->"
puts $HTML "<!-- Date: [exec date "+%a, %e %b %Y %X %Z"] -->"

puts $HTML "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">"
puts $HTML "<HTML VERSION=\"-//W3C//DTD HTML 3.2 Final//EN\">\n<HEAD>"
puts $HTML "<META NAME=\"author\" CONTENT=\"mkpixndx tcl script\">"

#Use the remainder of the command tail as the html page title
#overridden by TITLE entry in "captions" file if present
if {[lsearch $capslist TITLE] != -1} { 
   set title $head(TITLE)
   } else {
   set title [lrange $argv 1 end]
}
if {![string length $title]} {set title "Picture Catalogue"}
puts $HTML "<TITLE>$title</TITLE>\n</HEAD>\n\n<BODY bgcolor=\"#A06000\">"

#Write out a heading (here it's the same as the page title)
puts $HTML "<hr>\n<H1 align=center>$title</H1>\n<hr>\n" 

#Go to the directory with the pictures in it
cd $filename

#Start table to format page contents
puts $HTML "<div align=center><TABLE><TR>\n"

#Work through a list of all the the jpegs ...
foreach pic [lsort [glob *.\[Jj\]\[Pp\]*\[Gg\]]] {

#get the file size
   set size [lindex [exec ls -s $pic] 0]K
   
#Make corresponding thumbnail if not already present
   if {[file exists thumbnails/th_$pic] \
         && [file mtime $pic] < [file mtime thumbnails/th_$pic]} {
      puts "$pic -- thumbnail already present"
   } else {
      puts "Creating thumbnail of $pic"
      exec cp $pic thumbnails/th_$pic
      exec mogrify -geometry 256x512 thumbnails/th_$pic
   }

#Write the html code for each picture

   set cap [expr [lsearch $capslist $pic] != -1]
   puts $HTML "<TD><A HREF=\"$filename/$pic\">"
   puts $HTML "<IMG src=\"$filename/thumbnails/th_$pic\" ALT=\"$pic $size\""
   puts $HTML "BORDER=3 WIDTH=256 hspace=10 vspace=6 align=middle></A></TD>"
   puts $HTML "<TD align=center valign=middle width=256>"
   if $cap {puts $HTML "<H3>$head($pic)</H3>"}   
   puts $HTML "<H3><A href=\"$filename/$pic\">$pic $size</A></H3>"
   if $cap {puts $HTML "<P align=justify>$text($pic)</P></TD>"}

   puts $HTML "\n</TR><TR>\n"
}

#End the loop and change back to the directory you came from
cd ..

#Close table
puts $HTML "</TR>\n</TABLE>\n</div>"

#Finally write out the page bottom ...
puts $HTML "<HR>\n<div align=center>"
puts $HTML "<TABLE BORDER=\"0\" WIDTH=\"90%\">"
puts $HTML "<TR>\n<TD WIDTH=\"20%\"><A HREF=\"$url\">home</A></td>"
puts $HTML "<TD WIDTH=\"10%\"></TD>"
puts $HTML "<TD WIDTH=\"40%\" align=center><em>Updated : \
<!-- datestamp:begin -->[exec date "+%e-%b-%Y %R %Z"]<!-- datestamp:end --></em></TD>"
puts $HTML "<TD WIDTH=\"10%\"></TD>"
puts $HTML "<TD WIDTH=\"20%\" align=right>\
<A HREF=\"mailto:$email\";>email</A></td>"

#Finish up the html code ...
puts $HTML "</TR>\n</TABLE>\n</div>\n<HR>\n</BODY>\n</HTML>\n"

#And close the file
close $HTML

#End of File

Lynx friendly