#!/usr/bin/python # Make the thumbnail file "index.html" for images in all immediate subdirectories import sys import os import string import urllib import Image import ImageDraw def myexec(cmd,args): """Execute the command""" ret = os.spawnvp(os.P_WAIT,cmd,[cmd]+args) if (ret!=0): print("error: executing "+str([cmd]+args)+" returned "+str(ret)+"\n") sys.exit(1) def thumbnail(fn): """Makes an 80x80 thumbnail out of the fn, and returns it as an im""" print(fn) im = Image.open(fn) if (im.mode!="RGB"): im = im.convert("RGB") width,height = im.size # a roughly-aspect-preserving scale so the minimum dimension is 80pixels cropsize = 80 scalew,scaleh = (float(cropsize)/width, float(cropsize)/height) scale = max(scalew,scaleh) sw,sh = (min(int(width*scale+0.5),cropsize*3), min(int(height*scale+0.5),cropsize*3)) im.thumbnail((sw,sh), Image.ANTIALIAS) # now crop it to the central 80x80 cx,cy = (int((sw-cropsize)/2), int((sh-cropsize)/2) ) im2 = im.crop( (cx,cy,cx+cropsize,cy+cropsize) ) del im return im2 def titleblock(caption): """Draws an 80x80 block with the specified text centered in it""" im = Image.new("RGB",(80,80)) draw = ImageDraw.Draw(im) # caption is a list of nonblanks separated by blanks. words = caption.split() # for each word calculate its size szwords = [] height = 0 for s in words: (w,h) = draw.textsize(s) szwords.append( (w,h,s) ) if (h>height): height=h (sepw,seph) = draw.textsize(" ") # build up rows rows = [] row = [] pos = 0 for (w,h,s) in szwords: if (pos==0): row += [(w,s)] pos += w elif (pos+sepw+w<80): row += [(sepw," "),(w,s)] pos += sepw+w else: rows += [(pos,row)] row = [(w,s)] pos = w if (pos>0): rows += [(pos,row)] # figure out heights numrows = len(rows) heightall = numrows*height y = (80-heightall)/2 # now draw each word for (rw,row) in rows: x = (80-rw)/2 for (w,s) in row: draw.text( (x,y),s ) x += w y += height del draw return im def map(caption,width,height,ifiles,jpegfn,anchor): """Makes an imagemap out of fns, saves it to file mapfn, and returns a ...""" im = Image.new("RGB",(width*80,height*80)) s = "\n" x,y = 0,0 for fn in ifiles: try: if (fn=="#index"): tim = titleblock(caption) fn = caption+"/" else: tim = thumbnail(fn) except IOError: tim = Image.new("RGB",(80,80)) im.paste(tim,(x*80,y*80)) del tim s += "\n" x += 1 if (x>=12): x=0 y += 1 im.save(jpegfn,"JPEG",quality=40) s += "\n" return s def dodir(dir, files, pos): """Ensure that the directory's thumbnails are there, and return an integer for the following dir's pos Dir is the directory to examine. HTML we generate will point to that directory. files is a list of the files in that directory. Pos is where we start. We return a tuple (shtml,int) for the html to display this, and the pos for the next one. As a side effect we create some files in the directory.""" # was it already there? prefix = dir+"/.thumb."+str(pos) (dir2,caption) = os.path.split(dir) if (os.path.exists(prefix+".shtml") and os.path.exists(prefix+".next")): f = file(prefix+".next","r") p2 = f.read() f.close() f = file(prefix+".shtml","r") shtml = f.read() f.close() return (shtml, int(p2)) # if not, we have to generate it print dir+"."+str(pos) # first figure out all the filenames ifiles = ["#index"] for fn in files: root,ext = os.path.splitext(fn) ext = string.lower(ext) if (ext!=".jpg" and ext!=".jpeg" and ext!=".gif" and ext!=".tif" and ext!=".tiff" and ext!=".bmp"): continue if (fn[0:1]=="."): continue ifiles.append(dir+"/"+fn) # figure out how we'll build our three chunks numthumbs = len(ifiles) if (pos==0 or numthumbs+pos<=12): numa = 0 else: numa = 12-pos numthumbs-=numa numc = numthumbs % 12 numb = numthumbs - numc numbrows = numb/12 nextpos = numc if (numa==0 and numb==0): nextpos = (pos+numc)%12 ifilesa = ifiles[0:numa] ifilesb = ifiles[numa:numa+numb] ifilesc = ifiles[numa+numb:] # and build them shtml = "" if (numa>0): anchor,tfn = (prefix+".a", prefix+".a.jpg") shtml += "
\n" shtml += map(caption,numa,1,ifilesa,tfn,anchor) if (numb>0): anchor,tfn = (prefix+".b", prefix+".b.jpg") shtml += "
\n" shtml += map(caption,12,numbrows,ifilesb,tfn,anchor) if (numc>0): anchor,tfn = (prefix+".c", prefix+".c.jpg") shtml += "" shtml += map(caption,numc,1,ifilesc,tfn,anchor).replace("\n","") # save the output for future f = file(prefix+".shtml","w") f.write(shtml) f.close() f = file(prefix+".next","w") f.write(str(nextpos)) f.close() return (shtml,nextpos) def main(): pos = 0 html = "\n\nPictures\n\n\n
\n" for dir,dirs,files in os.walk("."): dirs.sort() files.sort() dir = os.path.normpath(dir); #remove leading "./" (head,tail) = os.path.split(dir) if (dir=="" or tail[:1]=="."): continue (shtml,pos2) = dodir(dir,files,pos) pos = pos2 html += shtml html += "\n
\n" f = file("thumbs.html","w") f.write(html) f.close() print("thumbs.html") slideshow_src = """ Slideshow
<  >
""" try: if not os.path.exists("slideshow.html"): f = open("slideshow.html","w") f.write(slideshow_src) f.close() print("slideshow.html") main() except KeyboardInterrupt: print("");