#!/usr/bin/python # makeshow: creates a slide show for a directory containing .jpg # images and their captions. # # Copyright Luca de Alfaro , 2005. # # Permission is hereby granted, without written agreement and without # license or royalty fees, to use, copy, modify, and distribute this # software and its documentation for any purpose, provided that the # above copyright notice and the following two paragraphs appear in # all copies of this software. # # IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, # INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT # OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE # AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN # "AS IS" BASIS, AND THE AUTHOR HAS NO OBLIGATION TO PROVIDE # MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. import sys, string, os, shutil STATSIZE = 6 # Positional argument for stat size qual = ["100", "85", "90"] imgsize = ["1600", "800", "1200"] title = "Photo gallery" # Default title index_name = "index.html" thumb_size = "150" # Default size of the thumbnail. max_img_line = 4 # N. of images in a line alt_description = "Thumbnail" # Default alt tag. thumbs_prefix = "thumbs/" size_prefix = ["", "size1/", "size2/"] size_up_prefix = ["", "../", "../"] # Pieces of the html to build pretitle = '\n' posttitle = "" posttitle += '\n' posttitle += " \n" def make_title (title) : return (pretitle + "" + title + "\n" + posttitle) def make_start (title) : return ("

" + title + "

\n


\n
\n\n\n\n
") inter_image = "" new_row = "
" the_end = '

Up One Level\n' # This produces all the html code for a single image. # It does _not_ resize the image and prepare the thumbnail; # that should be done in advance. def make_image (image_text, image_dest, html_dest) : img_code = ('
' + alt_description + '
' + '
' + 'Small' + ' Medium' + ' Large' + '
') return (img_code) # Resizes an image, ensuring the result is in jpg. def resize_image (file_source, file_dest, quality, imgsize): cs0 = "nice convert -quality " + quality cs1 = " -resize 1400x1000 " cs2 = file_source + " " + file_dest os.system (cs0 + cs1 + cs2) # Resizes an image, ensuring the result fits into a small screen. def resize_small_image (file_source, file_dest, quality, imgsize): cs0 = "nice convert -quality " + quality cs1 = " -resize 800x600 " cs2 = file_source + " " + file_dest os.system (cs0 + cs1 + cs2) # Makes a thumbnail. def make_thumbnail (file_source, file_dest, ts): cs0 = "nice convert -size " + ts + "x" + ts + " " + file_source cs1 = " -resize " + ts + "x" + ts + " +profile '*' -quality 75 " + file_dest os.system (cs0 + cs1) # Prepares name of text file def text_name (f): fparts = string.split (f, ".") return fparts[0] + '.txt' # Prepares an html encoding of the text def prepare_text (fname): try: f = open (fname) except IOError: return ('') text = '' while 1: l = f.readline() if l == '': break # Removes the final end of line if l[-1:] == '\n': l = l[:-1] text = text + '
' + l + '
' f.close() return (text) def make_slide (size_idx, img, html, text, img_idx) : # First, computes the links. # Up link up_link = size_up_prefix [size_idx] + index_name # Next image if img_idx == len (ffiles) - 1: # Last one: points up next_link = up_link else: # Not the last one: points to the next one next_f = ffiles [f_idx + 1] f_parts = string.split (next_f, ".") next_link = f_parts[0] + ".html" # Previous image if img_idx == 0: # First one: points up prev_link = up_link else: # Not the first one: points to the previous one prev_f = ffiles [f_idx - 1] f_parts = string.split (prev_f, ".") prev_link = f_parts[0] + ".html" # Go to the same image in other sizes gosize = ["", "", ""] for i in range(3): gosize [i] = size_up_prefix [size_idx] + size_prefix [i] + html # starts the html file. f = open (size_dir [size_idx] + html, "w") f.write (make_title (img)) # Writes the image f.write ('
\n') # Writes the buttons below the image f.write ('
') f.write (' Previous ') f.write (' Up ') f.write (' Next ') f.write ('     \n') f.write (' Small ') f.write (' Medium ') f.write (' Large ') f.write ('
\n') # Writes the text f.write ('

\n' + text + '\n') # Concludes the image f.write ('\n') def Usage() : print "Usage:", sys.argv[0], "[-title 'string'] [-quality 0..100] [-size imgsize] [-alt 'string']

" print print "If a file xyz.jpg is present in , then", sys.argv[0], print "will look for xyz.txt which, if found, will become the caption for xyz.jpg." sys.exit(1) if len(sys.argv) <= 1 : Usage() sys.exit (1) argp = 1 while argp < len(sys.argv) and sys.argv[argp][0:1] == "-" : if sys.argv[argp] == "-quality" : qual [2] = sys.argv[argp+1] print "New medium quality will be ", qual [2] argp = argp + 2 elif sys.argv[argp] == "-title" : title = sys.argv[argp+1] print "Title will be ", title argp = argp + 2 elif sys.argv[argp] == "-alt" : alt_description = sys.argv[argp+1] print "Alt tag will be ", alt_description argp = argp + 2 else : Usage() sys.exit (1) if len(sys.argv) != argp + 1: Usage() sys.exit (1) # Figures out the directories source_dir = sys.argv [argp] thumbs_dir = source_dir + "/" + thumbs_prefix size_dir = ["", "", ""] size_dir [1] = source_dir + "/" + size_prefix [1] size_dir [2] = source_dir + "/" + size_prefix [2] size_dir [0] = source_dir + "/" + size_prefix [0] # Makes the destination directory, if it doesn't already exists. try: os.remove (thumbs_dir + '*') except OSError: pass try: os.remove (size_dir [1] + '*') except OSError: pass try: os.remove (size_dir [2] + '*') except OSError: pass try: os.rmdir (thumbs_dir) except OSError: pass try: os.rmdir (size_dir [1]) except OSError: pass try: os.rmdir (size_dir [2]) except OSError: pass try: os.remove (source_dir + '/*.html') except OSError: pass try: os.mkdir (thumbs_dir) except OSError: pass try: os.mkdir (size_dir [1]) except OSError: pass try: os.mkdir (size_dir [2]) except OSError: pass # Now, gets the list of the files that we must include in the web page. files = os.listdir (source_dir) ffiles = [] # Filters only files in jpg and png formats. for f in files: fparts = string.split (f, ".") if len(fparts) == 2 and (fparts[1] == "jpg" or fparts[1] == "png" or fparts[1] == "JPG" or fparts[1] == "PNG"): ffiles.append (f) # Ok, now in ffiles I have only jpg and png formats. # I must sort the list. ffiles.sort () # Writes the head of the html document. findex = open (source_dir + "/" + index_name, "w") findex.write (make_title (title)) findex.write (make_start (title)) # Now we must get the images one by one, and: # first resize them and create the thumbnail, # then output the html code. images_in_line = 0 # Counts how many images have been output on the line image_count = 0 # Counts how many images in total have been output tot_images = len(ffiles) # Total n. of images for f_idx in range (len (ffiles)): f = ffiles [f_idx] print "Processing ", f # Ensures resulting name is *.jpg f_parts = string.split (f, ".") if f_parts[1] == "png": f_parts[1] = "jpg" file_dest = f_parts[0] + "." + f_parts[1] html_dest = f_parts[0] + ".html" # Builds the paths to the ... src_f = size_dir [0] + f # source file dest1_f = size_dir [1] + file_dest # destination file 1 dest2_f = size_dir [2] + file_dest # destination file 2 thumb_f = thumbs_dir + file_dest # thumbnail file # Resizes the file resize_small_image (src_f, dest1_f, qual [1], imgsize [1]) resize_image (src_f, dest2_f, qual [2], imgsize [2]) make_thumbnail (dest1_f, thumb_f, thumb_size) # Prepares the text text_f = text_name(f) # name of text file text = prepare_text (size_dir [0] + text_f) # Now outputs the html code for the thumbnail dest_size = os.path.getsize (dest2_f) orig_size = os.path.getsize (src_f) findex.write (make_image (text, file_dest, html_dest)) # and for the slide show, in the three sizes make_slide (0, file_dest, html_dest, text, f_idx) make_slide (1, file_dest, html_dest, text, f_idx) make_slide (2, file_dest, html_dest, text, f_idx) images_in_line += 1 image_count += 1 if image_count == tot_images: findex.write (the_end) elif images_in_line == max_img_line: findex.write (new_row) images_in_line = 0 else: findex.write (inter_image) # Closes the file findex.close ()