emin

changeset 28
1088bb11189f
parent 26
79167c58c3a5
     1.1 --- a/emin	Wed Apr 24 10:49:54 2013 +0200
     1.2 +++ b/emin	Wed Feb 26 12:48:21 2020 +0000
     1.3 @@ -1,7 +1,7 @@
     1.4 -#! /usr/bin/env python
     1.5 +#! /usr/bin/env python3
     1.6  # -*- python -*-
     1.7  
     1.8 -"""%prog [opts] dir [outdir]
     1.9 +"""%prog [opts] dir [outdir=./gallery]
    1.10  
    1.11  emin - a static web gallery builder
    1.12  
    1.13 @@ -39,7 +39,7 @@
    1.14  """
    1.15  
    1.16  __author__ = "Andy Buckley <andy@insectnation.org>"
    1.17 -__version__ = "0.3.4"
    1.18 +__version__ = "0.4.0"
    1.19  
    1.20  
    1.21  import logging
    1.22 @@ -60,7 +60,7 @@
    1.23                    help="max number of thumbnail columns on one page (default: %default)")
    1.24  parser.add_option("-r", "--num-rows", "--rows", dest="NUM_ROWS", default=6, type=int,
    1.25                    help="max number of thumbnail rows on one page (default: %default). Set < 1 for unlimited (i.e. all on one page)")
    1.26 -parser.add_option("--thumb-height", dest="THUMB_HEIGHT", default=150, type=int,
    1.27 +parser.add_option("--thumb-height", dest="THUMB_HEIGHT", default=250, type=int,
    1.28                    help="thumbnail height, in pixels (default: %default)")
    1.29  parser.add_option("--max-imgsize", dest="MAX_IMGSIZE", default=800, type=int,
    1.30                    help="max large image dimension in pixels (default: %default)")
    1.31 @@ -86,13 +86,13 @@
    1.32  import sys, os, glob, re, math, shutil, fnmatch
    1.33  try:
    1.34      from Cheetah.Template import Template
    1.35 -except Exception, e:
    1.36 +except Exception as e:
    1.37      logging.error("Couldn't import required Cheetah package")
    1.38      exit(1)
    1.39  try:
    1.40      import PIL.Image as PILI
    1.41 -except Exception, e:
    1.42 -    logging.error("Couldn't import required Python Imaging Library package")
    1.43 +except Exception as e:
    1.44 +    logging.error("Couldn't import required Python Imaging Library / Pillow package")
    1.45      exit(1)
    1.46  
    1.47  
    1.48 @@ -102,7 +102,7 @@
    1.49      exit(1)
    1.50  if len(args) >= 1:
    1.51      opts.SRCDIR = os.path.normpath(args[0])
    1.52 -    opts.OUTDIR = os.path.normpath(args[0])
    1.53 +    opts.OUTDIR = os.path.normpath("./gallery") #args[0])
    1.54  if len(args) == 2:
    1.55      opts.OUTDIR = os.path.normpath(args[1])
    1.56  
    1.57 @@ -150,9 +150,9 @@
    1.58  ## Go to the gallery directory and test if it's writeable
    1.59  if not os.access(opts.OUTDIR, os.W_OK):
    1.60      try:
    1.61 -        logging.info("Making output dir in %s" % opts.OUTDIR)
    1.62 +        logging.debug("Making output dir in %s" % opts.OUTDIR)
    1.63          os.makedirs(opts.OUTDIR)
    1.64 -    except Exception, e:
    1.65 +    except Exception as e:
    1.66          logging.error("Problem when making output dir %s... exiting" % opts.OUTDIR)
    1.67          exit(1)
    1.68  
    1.69 @@ -162,9 +162,9 @@
    1.70  THUMBDIR = os.path.join(opts.OUTDIR, opts.THUMBDIR)
    1.71  try:
    1.72      if not os.path.isdir(THUMBDIR):
    1.73 -        logging.info("Making thumbs dir in %s" % THUMBDIR)
    1.74 +        logging.debug("Making thumbs dir in %s" % THUMBDIR)
    1.75          os.makedirs(THUMBDIR)
    1.76 -except Exception, e:
    1.77 +except Exception as e:
    1.78      logging.error("Problem when making thumbnails dir... exiting")
    1.79      exit(1)
    1.80  
    1.81 @@ -212,7 +212,8 @@
    1.82          shutil.copy(imgpath, targetpath)
    1.83  
    1.84  
    1.85 -## Store some image info
    1.86 +## Process images
    1.87 +logging.info("Processing {:d} images...".format(len(outimgs)))
    1.88  imgsinfo = {}
    1.89  for picpath in outimgs:
    1.90      picname = os.path.basename(picpath)
    1.91 @@ -223,34 +224,43 @@
    1.92      picpathparts = os.path.splitext(picpath)
    1.93      picnameparts = os.path.splitext(picname)
    1.94      extn = picnameparts[1].lower()
    1.95 +    pictype = extn[1:].upper(); pictype = "TIFF" if pictype == "TIF" else pictype
    1.96 +    picversions[pictype] = picname
    1.97      convcmd = None
    1.98 -    if extn in [".tif", ".tiff"]:
    1.99 -        picversions["TIFF"] = picname
   1.100 -        newpicname = picname + ".jpg"
   1.101 -        newpicpath = picpath + ".jpg"
   1.102 -        picversions["JPG"] = newpicname
   1.103 -        convcmd = ["convert", picpath, newpicpath]
   1.104 +    if pictype in ["TIFF", "EPS"]:
   1.105 +        newpictype = "PNG" if pictype == "EPS" else "JPG"
   1.106 +        newextn = "." + newpictype.lower()
   1.107 +        newpicname = picname + newextn
   1.108 +        newpicpath = picpath + newextn
   1.109 +        picversions[newpictype] = newpicname
   1.110 +        logging.debug("Converting {} to {}".format(picpath, newpicpath))
   1.111 +        img = PILI.open(picpath, "r")
   1.112 +        img.save(newpicpath)
   1.113          picname = newpicname
   1.114          picpath = newpicpath
   1.115 -    elif extn in [".eps", ".pdf"]:
   1.116 -        picversions[extn[1:].upper()] = picname
   1.117 +    elif extn in [".pdf"]:
   1.118          newpicname = picname + ".png"
   1.119          newpicpath = picpath + ".png"
   1.120          picversions["PNG"] = newpicname
   1.121 -        convcmd = ["convert", "-density", "200", "-resize", "800x700", picpath, newpicpath]
   1.122 +        logging.debug("Converting {} to {}".format(picpath, newpicpath))
   1.123 +        try:
   1.124 +            import pdf2image, tempfile
   1.125 +            with tempfile.TemporaryDirectory() as tmppath:
   1.126 +                pdfimgs = pdf2image.convert_from_path(picpath, output_folder=tmppath, first_page=0, last_page=1) #, fmt="png")
   1.127 +                #pdfimgs = pdf2image.convert_from_path(picpath, output_folder=tmppath)
   1.128 +                pdfimg = pdfimgs[0].save(newpicpath)
   1.129 +                if len(pdfimgs) > 1:
   1.130 +                    logging.warning("Multi-page PDF {}: showing only page 1".format(picpath))
   1.131 +        except ImportError:
   1.132 +            import subprocess
   1.133 +            try:
   1.134 +                convcmd = ["convert", "-density", "200", "-resize", "800x700", picpath, newpicpath]
   1.135 +                subprocess.check_call(convcmd)
   1.136 +            except:
   1.137 +                logging.error("PDF conversion failed for {}... skipping".format(picpath))
   1.138 +                continue
   1.139          picname = newpicname
   1.140          picpath = newpicpath
   1.141 -    else:
   1.142 -        picversions[extn[1:].upper()] = picname
   1.143 -
   1.144 -    ## Do the conversion
   1.145 -    if convcmd:
   1.146 -        try:
   1.147 -            # TODO: threading / multiprocessing for speed-up?
   1.148 -            import subprocess
   1.149 -            subprocess.check_call(convcmd)
   1.150 -        except:
   1.151 -            raise
   1.152  
   1.153      ## Main pic info
   1.154      info = ImageInfo()
   1.155 @@ -269,13 +279,12 @@
   1.156      ## TODO: Be lazy!
   1.157      #if opts.FORCE or not os.access(thumbpath, os.R_OK) or os.stat(thumbpath).st_mtime > os.stat(pic).st_mtime:
   1.158      try:
   1.159 -        logging.debug("Making new thumbnail %s for %s (max height %d)" % \
   1.160 -                          (thumbpath, picname, opts.THUMB_HEIGHT))
   1.161 +        logging.debug("Making new thumbnail %s for %s (max height %d)" % (thumbpath, picname, opts.THUMB_HEIGHT))
   1.162          thumbimg = PILI.open(picpath, "r")
   1.163          thumbimg.thumbnail((100000000, opts.THUMB_HEIGHT), resample=PILI.ANTIALIAS)
   1.164          thumbimg.save(thumbpath)
   1.165          info.thumbsize = thumbimg.size
   1.166 -    except Exception, e:
   1.167 +    except Exception as e:
   1.168          logging.warning("Problem when making thumbnail from %s... exiting" % picpath)
   1.169          exit(1)
   1.170  
   1.171 @@ -419,9 +428,9 @@
   1.172        <tr/><tr>
   1.173        #end if
   1.174        #set info = $PICINFO[$thumb]
   1.175 -      <td>
   1.176 +      <td style="text-align:right;">
   1.177          <a href="$info.relpath" $jsrel><img alt="$thumb" src="$info.relthumbpath" style="border:0;" width="$info.thumbx" height="$info.thumby" /></a><br/>
   1.178 -        #for fmt, name in $info.versions.iteritems()
   1.179 +        #for fmt, name in $info.versions.items()
   1.180          <a class="format" href="$name">$fmt.lower()</a>
   1.181          #end for
   1.182        </td>
   1.183 @@ -484,7 +493,7 @@
   1.184  #     print THUMBPATHS
   1.185  #     THUMBDIMS = [t.size for t in thumbsinfo.values()[pics_start : pics_end]]
   1.186  
   1.187 -    logging.info("Writing to index file %s" % PAGEPATH)
   1.188 +    logging.debug("Writing to index file %s" % PAGEPATH)
   1.189      f = open(PAGEPATH, "w")
   1.190      logging.debug("Images on page: %s" % PAGEPICS)
   1.191      tdict = {}

mercurial