Update to the 2020s default tip

Wed, 26 Feb 2020 12:48:21 +0000

author
Andy Buckley <andy@insectnation.org>
date
Wed, 26 Feb 2020 12:48:21 +0000
changeset 28
1088bb11189f
parent 27
4ce678884842

Update to the 2020s

.hgignore file | annotate | diff | comparison | revisions
emin file | annotate | diff | comparison | revisions
setup.py file | annotate | diff | comparison | revisions
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.hgignore	Wed Feb 26 12:48:21 2020 +0000
     1.3 @@ -0,0 +1,4 @@
     1.4 +test.*$
     1.5 +build
     1.6 +dist
     1.7 +.*egg-info.*
     2.1 --- a/emin	Wed Apr 24 10:49:54 2013 +0200
     2.2 +++ b/emin	Wed Feb 26 12:48:21 2020 +0000
     2.3 @@ -1,7 +1,7 @@
     2.4 -#! /usr/bin/env python
     2.5 +#! /usr/bin/env python3
     2.6  # -*- python -*-
     2.7  
     2.8 -"""%prog [opts] dir [outdir]
     2.9 +"""%prog [opts] dir [outdir=./gallery]
    2.10  
    2.11  emin - a static web gallery builder
    2.12  
    2.13 @@ -39,7 +39,7 @@
    2.14  """
    2.15  
    2.16  __author__ = "Andy Buckley <andy@insectnation.org>"
    2.17 -__version__ = "0.3.4"
    2.18 +__version__ = "0.4.0"
    2.19  
    2.20  
    2.21  import logging
    2.22 @@ -60,7 +60,7 @@
    2.23                    help="max number of thumbnail columns on one page (default: %default)")
    2.24  parser.add_option("-r", "--num-rows", "--rows", dest="NUM_ROWS", default=6, type=int,
    2.25                    help="max number of thumbnail rows on one page (default: %default). Set < 1 for unlimited (i.e. all on one page)")
    2.26 -parser.add_option("--thumb-height", dest="THUMB_HEIGHT", default=150, type=int,
    2.27 +parser.add_option("--thumb-height", dest="THUMB_HEIGHT", default=250, type=int,
    2.28                    help="thumbnail height, in pixels (default: %default)")
    2.29  parser.add_option("--max-imgsize", dest="MAX_IMGSIZE", default=800, type=int,
    2.30                    help="max large image dimension in pixels (default: %default)")
    2.31 @@ -86,13 +86,13 @@
    2.32  import sys, os, glob, re, math, shutil, fnmatch
    2.33  try:
    2.34      from Cheetah.Template import Template
    2.35 -except Exception, e:
    2.36 +except Exception as e:
    2.37      logging.error("Couldn't import required Cheetah package")
    2.38      exit(1)
    2.39  try:
    2.40      import PIL.Image as PILI
    2.41 -except Exception, e:
    2.42 -    logging.error("Couldn't import required Python Imaging Library package")
    2.43 +except Exception as e:
    2.44 +    logging.error("Couldn't import required Python Imaging Library / Pillow package")
    2.45      exit(1)
    2.46  
    2.47  
    2.48 @@ -102,7 +102,7 @@
    2.49      exit(1)
    2.50  if len(args) >= 1:
    2.51      opts.SRCDIR = os.path.normpath(args[0])
    2.52 -    opts.OUTDIR = os.path.normpath(args[0])
    2.53 +    opts.OUTDIR = os.path.normpath("./gallery") #args[0])
    2.54  if len(args) == 2:
    2.55      opts.OUTDIR = os.path.normpath(args[1])
    2.56  
    2.57 @@ -150,9 +150,9 @@
    2.58  ## Go to the gallery directory and test if it's writeable
    2.59  if not os.access(opts.OUTDIR, os.W_OK):
    2.60      try:
    2.61 -        logging.info("Making output dir in %s" % opts.OUTDIR)
    2.62 +        logging.debug("Making output dir in %s" % opts.OUTDIR)
    2.63          os.makedirs(opts.OUTDIR)
    2.64 -    except Exception, e:
    2.65 +    except Exception as e:
    2.66          logging.error("Problem when making output dir %s... exiting" % opts.OUTDIR)
    2.67          exit(1)
    2.68  
    2.69 @@ -162,9 +162,9 @@
    2.70  THUMBDIR = os.path.join(opts.OUTDIR, opts.THUMBDIR)
    2.71  try:
    2.72      if not os.path.isdir(THUMBDIR):
    2.73 -        logging.info("Making thumbs dir in %s" % THUMBDIR)
    2.74 +        logging.debug("Making thumbs dir in %s" % THUMBDIR)
    2.75          os.makedirs(THUMBDIR)
    2.76 -except Exception, e:
    2.77 +except Exception as e:
    2.78      logging.error("Problem when making thumbnails dir... exiting")
    2.79      exit(1)
    2.80  
    2.81 @@ -212,7 +212,8 @@
    2.82          shutil.copy(imgpath, targetpath)
    2.83  
    2.84  
    2.85 -## Store some image info
    2.86 +## Process images
    2.87 +logging.info("Processing {:d} images...".format(len(outimgs)))
    2.88  imgsinfo = {}
    2.89  for picpath in outimgs:
    2.90      picname = os.path.basename(picpath)
    2.91 @@ -223,34 +224,43 @@
    2.92      picpathparts = os.path.splitext(picpath)
    2.93      picnameparts = os.path.splitext(picname)
    2.94      extn = picnameparts[1].lower()
    2.95 +    pictype = extn[1:].upper(); pictype = "TIFF" if pictype == "TIF" else pictype
    2.96 +    picversions[pictype] = picname
    2.97      convcmd = None
    2.98 -    if extn in [".tif", ".tiff"]:
    2.99 -        picversions["TIFF"] = picname
   2.100 -        newpicname = picname + ".jpg"
   2.101 -        newpicpath = picpath + ".jpg"
   2.102 -        picversions["JPG"] = newpicname
   2.103 -        convcmd = ["convert", picpath, newpicpath]
   2.104 +    if pictype in ["TIFF", "EPS"]:
   2.105 +        newpictype = "PNG" if pictype == "EPS" else "JPG"
   2.106 +        newextn = "." + newpictype.lower()
   2.107 +        newpicname = picname + newextn
   2.108 +        newpicpath = picpath + newextn
   2.109 +        picversions[newpictype] = newpicname
   2.110 +        logging.debug("Converting {} to {}".format(picpath, newpicpath))
   2.111 +        img = PILI.open(picpath, "r")
   2.112 +        img.save(newpicpath)
   2.113          picname = newpicname
   2.114          picpath = newpicpath
   2.115 -    elif extn in [".eps", ".pdf"]:
   2.116 -        picversions[extn[1:].upper()] = picname
   2.117 +    elif extn in [".pdf"]:
   2.118          newpicname = picname + ".png"
   2.119          newpicpath = picpath + ".png"
   2.120          picversions["PNG"] = newpicname
   2.121 -        convcmd = ["convert", "-density", "200", "-resize", "800x700", picpath, newpicpath]
   2.122 +        logging.debug("Converting {} to {}".format(picpath, newpicpath))
   2.123 +        try:
   2.124 +            import pdf2image, tempfile
   2.125 +            with tempfile.TemporaryDirectory() as tmppath:
   2.126 +                pdfimgs = pdf2image.convert_from_path(picpath, output_folder=tmppath, first_page=0, last_page=1) #, fmt="png")
   2.127 +                #pdfimgs = pdf2image.convert_from_path(picpath, output_folder=tmppath)
   2.128 +                pdfimg = pdfimgs[0].save(newpicpath)
   2.129 +                if len(pdfimgs) > 1:
   2.130 +                    logging.warning("Multi-page PDF {}: showing only page 1".format(picpath))
   2.131 +        except ImportError:
   2.132 +            import subprocess
   2.133 +            try:
   2.134 +                convcmd = ["convert", "-density", "200", "-resize", "800x700", picpath, newpicpath]
   2.135 +                subprocess.check_call(convcmd)
   2.136 +            except:
   2.137 +                logging.error("PDF conversion failed for {}... skipping".format(picpath))
   2.138 +                continue
   2.139          picname = newpicname
   2.140          picpath = newpicpath
   2.141 -    else:
   2.142 -        picversions[extn[1:].upper()] = picname
   2.143 -
   2.144 -    ## Do the conversion
   2.145 -    if convcmd:
   2.146 -        try:
   2.147 -            # TODO: threading / multiprocessing for speed-up?
   2.148 -            import subprocess
   2.149 -            subprocess.check_call(convcmd)
   2.150 -        except:
   2.151 -            raise
   2.152  
   2.153      ## Main pic info
   2.154      info = ImageInfo()
   2.155 @@ -269,13 +279,12 @@
   2.156      ## TODO: Be lazy!
   2.157      #if opts.FORCE or not os.access(thumbpath, os.R_OK) or os.stat(thumbpath).st_mtime > os.stat(pic).st_mtime:
   2.158      try:
   2.159 -        logging.debug("Making new thumbnail %s for %s (max height %d)" % \
   2.160 -                          (thumbpath, picname, opts.THUMB_HEIGHT))
   2.161 +        logging.debug("Making new thumbnail %s for %s (max height %d)" % (thumbpath, picname, opts.THUMB_HEIGHT))
   2.162          thumbimg = PILI.open(picpath, "r")
   2.163          thumbimg.thumbnail((100000000, opts.THUMB_HEIGHT), resample=PILI.ANTIALIAS)
   2.164          thumbimg.save(thumbpath)
   2.165          info.thumbsize = thumbimg.size
   2.166 -    except Exception, e:
   2.167 +    except Exception as e:
   2.168          logging.warning("Problem when making thumbnail from %s... exiting" % picpath)
   2.169          exit(1)
   2.170  
   2.171 @@ -419,9 +428,9 @@
   2.172        <tr/><tr>
   2.173        #end if
   2.174        #set info = $PICINFO[$thumb]
   2.175 -      <td>
   2.176 +      <td style="text-align:right;">
   2.177          <a href="$info.relpath" $jsrel><img alt="$thumb" src="$info.relthumbpath" style="border:0;" width="$info.thumbx" height="$info.thumby" /></a><br/>
   2.178 -        #for fmt, name in $info.versions.iteritems()
   2.179 +        #for fmt, name in $info.versions.items()
   2.180          <a class="format" href="$name">$fmt.lower()</a>
   2.181          #end for
   2.182        </td>
   2.183 @@ -484,7 +493,7 @@
   2.184  #     print THUMBPATHS
   2.185  #     THUMBDIMS = [t.size for t in thumbsinfo.values()[pics_start : pics_end]]
   2.186  
   2.187 -    logging.info("Writing to index file %s" % PAGEPATH)
   2.188 +    logging.debug("Writing to index file %s" % PAGEPATH)
   2.189      f = open(PAGEPATH, "w")
   2.190      logging.debug("Images on page: %s" % PAGEPICS)
   2.191      tdict = {}
     3.1 --- a/setup.py	Wed Apr 24 10:49:54 2013 +0200
     3.2 +++ b/setup.py	Wed Feb 26 12:48:21 2020 +0000
     3.3 @@ -7,8 +7,7 @@
     3.4  It's primarily intended for making Web photo galleries for the sorts of people
     3.5  who don't want to install some PHP monstrosity just to put their photos
     3.6  online. On the assumption that most people will want to tweak their gallery's
     3.7 -appearance, the output is fully customisable using the Cheetah templating
     3.8 -engine.
     3.9 +appearance, the output is customisable using the Cheetah templating engine.
    3.10  
    3.11  Supported image formats are JPEG, PNG, GIF, TIFF, PDF and EPS, with the latter
    3.12  two being converted to PNG for Web display.  Image resizing, renaming and
    3.13 @@ -16,30 +15,21 @@
    3.14  set. Large image sets can be split over several pages.
    3.15  """
    3.16  
    3.17 +## Py3 comptibility
    3.18 +from __future__ import print_function
    3.19 +
    3.20  ## Get setuptools stuff
    3.21 -from distutils.core import setup
    3.22 -
    3.23 -
    3.24 -## Try to import Cheetah templating
    3.25 -try:
    3.26 -    from Cheetah.Template import Template
    3.27 -except Exception, e:
    3.28 -    logging.error("Couldn't import required Cheetah package: please install Cheetah first")
    3.29 -    exit(1)
    3.30 -
    3.31 -## Try to import Python Imaging Library
    3.32 -try:
    3.33 -    import PIL.Image
    3.34 -except Exception, e:
    3.35 -    logging.error("Couldn't import required Python Imaging Library package: please install PIL first")
    3.36 -    exit(1)
    3.37 +#from distutils.core import setup
    3.38 +from setuptools import setup
    3.39  
    3.40  
    3.41  ## Setup definition
    3.42  setup(name = 'emin',
    3.43 -      version = "0.3.4",
    3.44 +      version = "0.4.0",
    3.45        scripts = ['emin'],
    3.46        requires = ["Cheetah", "PIL"],
    3.47 +      install_requires = ["Cheetah3", "Pillow", "pdf2image"],
    3.48 +      python_requires = ">=3",
    3.49        author = 'Andy Buckley',
    3.50        author_email = 'andy@insectnation.org',
    3.51        #url = 'http://www.insectnation.org/projects/emin/',
    3.52 @@ -53,3 +43,25 @@
    3.53                     'Operating System :: OS Independent',
    3.54                     'Programming Language :: Python']
    3.55        )
    3.56 +
    3.57 +
    3.58 +## Try to import Cheetah templating
    3.59 +try:
    3.60 +    import Cheetah
    3.61 +    from Cheetah.Template import Template
    3.62 +except Exception as e:
    3.63 +    print("Couldn't import required Cheetah package")
    3.64 +    exit(1)
    3.65 +
    3.66 +## Try to import Python Imaging Library
    3.67 +try:
    3.68 +    import PIL.Image
    3.69 +except Exception as e:
    3.70 +    print("Couldn't import required Python Imaging Library package")
    3.71 +    exit(1)
    3.72 +
    3.73 +## Try to import pdf2image converter
    3.74 +try:
    3.75 +    import pdf2image
    3.76 +except Exception as e:
    3.77 +    print("Couldn't import optional pdf2image package")

mercurial