Can now make thumbnails

Fri, 07 Nov 2008 10:18:51 +0000

author
andy
date
Fri, 07 Nov 2008 10:18:51 +0000
changeset 5
9c52ea3cb310
parent 4
406ab3438918
child 6
fffee8292ba7

Can now make thumbnails

emin file | annotate | diff | comparison | revisions
     1.1 --- a/emin	Mon Oct 20 12:31:01 2008 +0000
     1.2 +++ b/emin	Fri Nov 07 10:18:51 2008 +0000
     1.3 @@ -14,9 +14,10 @@
     1.4  PDFs, graphs...
     1.5  
     1.6  As for the name, this is a program to make pretty 
     1.7 -crappy galleries, so it's named after a pretty
     1.8 -crappy artist. And, thankfully, it's not many characters
     1.9 -to type --- trebles all round!
    1.10 +crappy galleries, so it's named after a pretty crappy 
    1.11 +artist. And, thankfully, e-m-i-n is not many characters
    1.12 +to type (and they're all close together on my Colemak
    1.13 +keyboard layout) --- trebles all round!
    1.14  
    1.15  Usage: 
    1.16  %prog [-d|-F|-H|-h|-i|-I|-n|-r|-R|-t|-T|-v|-y|-z|-Z]
    1.17 @@ -42,36 +43,77 @@
    1.18  """
    1.19  
    1.20  
    1.21 +import logging
    1.22  from optparse import OptionParser, OptionGroup
    1.23  parser = OptionParser(usage=usage, version=version)
    1.24 -parser.add_option("-", "--", dest="", help="")
    1.25 -## Num per page / all on one page
    1.26 -## Zip file
    1.27 -## Num cols (or no cols)
    1.28 -## Title
    1.29 -## Description
    1.30 -## Thumb height (100)
    1.31 -## Image size
    1.32 -## File suffix
    1.33 -## JS
    1.34 -## Generate thumbs (+ force)
    1.35 -## Header & footer files; whole page / snippet; Cheetah format page template
    1.36 +parser.add_option("-t", "--title", dest="TITLE", default="", 
    1.37 +                  help="title of this gallery")
    1.38 +parser.add_option("--template", dest="TEMPLATE", default=None, 
    1.39 +                  help="specify the template file to be used for the index pages")
    1.40 +parser.add_option("--zipfile", dest="", default=None, 
    1.41 +                  help="name of zip archive file. Default is based on the title.")
    1.42 +parser.add_option("-Z", "--no-zipfile", action="store_false",
    1.43 +                  dest="WRITE_ZIPFILE", default=True, 
    1.44 +                  help="disable writing out of a zipped archive of photos from this gallery")
    1.45 +parser.add_option("-c", "--num-cols", dest="NUM_COLS", default=5, type=int,
    1.46 +                  help="max number of thumbnail columns on one page (default: 5)")
    1.47 +parser.add_option("-r", "--num-rows", dest="NUM_ROWS", default=6, type=int,
    1.48 +                  help="max number of thumbnail rows on one page (default: 6). Set < 1 for unlimited (i.e. all on one page)")
    1.49 +parser.add_option("--thumb-height", dest="THUMB_HEIGHT", default=150, type=int,
    1.50 +                  help="thumbnail height, in pixels (default: 100)")
    1.51 +parser.add_option("--max-imgsize", dest="MAX_IMGSIZE", default=800, type=int,
    1.52 +                  help="max large image dimension in pixels (default: 800)")
    1.53 +parser.add_option("--no-js", dest="USE_JS", action="store_false", default=True, 
    1.54 +                  help="disable use of funky JavaScript display stuff")
    1.55 +parser.add_option("--force", dest="FORCE", action="store_true", default=False, 
    1.56 +                  help="force creation of gallery: regen thumbnails etc.")
    1.57 +parser.add_option("--no-table", dest="USE_TABLE", action="store_false", default=True, 
    1.58 +                  help="don't use an HTML table for thumbnail presentation: just let the thumbs flow into the browser window")
    1.59 +verbgroup = OptionGroup(parser, "Verbosity control")
    1.60 +parser.add_option("-l", dest="NATIVE_LOG_STRS", action="append",
    1.61 +                  default=[], help="set a log level in the Rivet library")
    1.62 +verbgroup.add_option("-V", "--verbose", action="store_const", const=logging.DEBUG, dest="LOGLEVEL",
    1.63 +                     default=logging.INFO, help="print debug (very verbose) messages")
    1.64 +verbgroup.add_option("-Q", "--quiet", action="store_const", const=logging.WARNING, dest="LOGLEVEL",
    1.65 +                     default=logging.INFO, help="be very quiet")
    1.66 +parser.add_option_group(verbgroup)
    1.67  opts, args = parser.parse_args()
    1.68 -
    1.69 -## Configure log levels
    1.70 -logging.configRoot(level=opts.LOGLEVEL, format="$(message)")
    1.71 +logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
    1.72  
    1.73  
    1.74  ## More stdlib imports
    1.75 -import sys, os, shutils, glob, re, commands
    1.76 -import logging, math
    1.77 +import sys, os, glob, re, commands, math, shutil
    1.78 +import traceback
    1.79  
    1.80  
    1.81 +## TODO: These should be options...
    1.82 +opts.OUTDIR = None
    1.83 +opts.RENAME = False
    1.84 +opts.CONVERT = False
    1.85 +
    1.86 +
    1.87 +## Set processing/output dir
    1.88 +if len(args) != 1:
    1.89 +    #print parser.show_usage()
    1.90 +    sys.exit(1)
    1.91 +opts.SRCDIR = args[0]
    1.92 +if opts.OUTDIR == None:
    1.93 +    opts.OUTDIR = args[0]
    1.94 +
    1.95 +
    1.96 +## Try to import Cheetah templating
    1.97 +## TODO Should be closer to where it's used? Does default need Cheetah?
    1.98 +try:
    1.99 +    from Cheetah import Template
   1.100 +except Exception, e:
   1.101 +    logging.error("Couldn't import required Python Cheetah package")
   1.102 +    sys.exit(1)
   1.103 +
   1.104  ## Try to import Python Imaging Library
   1.105  try:
   1.106 -    import PIL
   1.107 +    import PIL.Image as PILI
   1.108  except Exception, e:
   1.109 -    logging.error()
   1.110 +    logging.error("Couldn't import required Python Imaging Library package")
   1.111      sys.exit(1)
   1.112  
   1.113  
   1.114 @@ -80,17 +122,17 @@
   1.115      newstr = s.replace(" ", "-").replace(",", "").replace("/", "").replace(".", "")
   1.116      return newstr
   1.117  
   1.118 -logging.debug("Title: $TITLE")
   1.119 -logging.debug("Write header: $writeHeader $headerFile")
   1.120 -logging.debug("Write footer: $writeFooter $footerFile")
   1.121 -logging.debug("Thumb height: $THUMBMAXHEIGHT")
   1.122 -logging.debug("Non-option args: $nonoptargs")
   1.123 +
   1.124 +logging.debug("Title: %s" % opts.TITLE)
   1.125 +logging.debug("Thumb height: %d" % opts.THUMB_HEIGHT)
   1.126 +
   1.127  
   1.128  ## Go to the gallery directory and test if it's writeable
   1.129  if not os.access(opts.OUTDIR, os.W_OK):
   1.130      logging.error("Directory %s is not writeable" % opts.OUTDIR)
   1.131      sys.exit(1)
   1.132  
   1.133 +
   1.134  ## Rename images
   1.135  if opts.RENAME:
   1.136      ## TODO: types: PNG/GIF, JPEG, PDF
   1.137 @@ -101,22 +143,53 @@
   1.138              logging.debug("Renaming %s -> %s" % (img, newname))
   1.139              shutils.mv(img, newname)
   1.140  
   1.141 +
   1.142  ## TODO: Convert unviewables to JPG/PNG (keep link to originals)
   1.143  if opts.CONVERT:
   1.144      pass
   1.145  
   1.146 +
   1.147  ## Build the list of pictures to display
   1.148 -pics = sorted(glob.glob(extensions))
   1.149 +EXTENSIONS = ["*.jpg", "*.jpeg", "*.tif", "*.tiff", 
   1.150 +              "*.png", "*.gif", "*.gif",
   1.151 +              "*.eps", "*.pdf"]
   1.152 +pics = []
   1.153 +ROOTDIR = os.getcwd()
   1.154 +os.chdir(opts.SRCDIR)
   1.155 +for e in EXTENSIONS:
   1.156 +    pics += glob.glob(e)
   1.157 +os.chdir(ROOTDIR)
   1.158 +print sorted(pics)
   1.159 +
   1.160  
   1.161  ## Make thumbnails if needed
   1.162 -os.makedirs(os.path.join(os.OUTDIR, "thumbs"))
   1.163 -for pic in pics:
   1.164 -    thumbpic = os.path.join("thumbs", pic)
   1.165 -    if opts.REGENTHUMBS:
   1.166 -        logging.debug("Making new thumbnail for %s (max height %d)" % (pic, opts.THUMBMAXHEIGHT))
   1.167 -        ## TODO: This!
   1.168 -        # convert $pic $thumbpic
   1.169 -        # mogrify -resize "x${THUMBMAXHEIGHT}>" $thumbpic
   1.170 +try:
   1.171 +    thumbdir = os.path.join(opts.OUTDIR, "thumbs")
   1.172 +    if not os.path.isdir(thumbdir):
   1.173 +        logging.info("Making thumbs dir in %s" % thumbdir)
   1.174 +        os.makedirs(thumbdir)
   1.175 +except Exception, e:
   1.176 +    logging.error("Problem when making thumbnails dir... exiting")
   1.177 +    #traceback.print_exc()
   1.178 +    sys.exit(1)
   1.179 +
   1.180 +try:
   1.181 +    for pic in pics:
   1.182 +        ## TODO: Un-hard-code PNG thumb format
   1.183 +        thumbpic = os.path.join(opts.OUTDIR, "thumbs", pic + ".png")
   1.184 +        if opts.FORCE or not os.access(thumbpic, os.R_OK) \
   1.185 +                or os.stat(thumbpic).st_mtime > os.stat(pic).st_mtime:
   1.186 +            logging.debug("Making new thumbnail %s for %s (max height %d)" % \
   1.187 +                          (thumbpic, pic, opts.THUMB_HEIGHT))
   1.188 +            thumbimg = PILI.open(os.path.join(opts.SRCDIR, pic), "r")
   1.189 +            thumbimg.thumbnail((100000000, opts.THUMB_HEIGHT), resample=PILI.ANTIALIAS)
   1.190 +            #thumbimg.show()
   1.191 +            thumbimg.save(thumbpic)
   1.192 +except Exception, e:
   1.193 +    logging.warning("Problem when making thumbnail... exiting")
   1.194 +    #traceback.print_exc()
   1.195 +    #sys.exit(1)
   1.196 +
   1.197  
   1.198  ## Count the pictures
   1.199  logging.debug("Number of pictures = %d" % len(pics))
   1.200 @@ -124,6 +197,10 @@
   1.201      logging.debug("No pictures from which to build a gallery...")
   1.202      sys.exit(2)
   1.203  
   1.204 +
   1.205 +sys.exit(0)
   1.206 +
   1.207 +
   1.208  ## Calculate how many pages will be needed
   1.209  ## TODO: allow all on one page
   1.210  NUM_PER_PAGE = opts.NUMX * opts.NUMY
   1.211 @@ -152,7 +229,7 @@
   1.212          ## Numbers
   1.213          for n in range(NUM_PAGES):
   1.214              if n != pagenum:
   1.215 -            out += '[<a href="%s">%d</a>] ' % (getPageFilename(n), n)
   1.216 +                out += '[<a href="%s">%d</a>] ' % (getPageFilename(n), n)
   1.217          else:
   1.218              out += "[%d]" % n
   1.219          ## Next

mercurial