Attachment 'Thumbnails.py'

Download

   1 # -*- coding: utf-8 -*-
   2 """
   3     MoinMoin - Thumbnails - make thumbnail versions of attached images
   4 
   5     @copyright: (c) Elias Pschernig
   6     @license: GNU GPL, see COPYING for details.
   7 """
   8 
   9 from MoinMoin.Page import Page
  10 from MoinMoin.action import AttachFile
  11 import os, re
  12 from MoinMoin import config
  13 
  14 try:
  15     from PIL import Image
  16 except ImportError:
  17     class Image:
  18         ANTIALIAS = 0
  19         def open(path):
  20             self = Image()
  21             self.path = path
  22             output = os.popen("identify '%s'" % self.path).read()
  23             mob = re.compile(r" (\d+)x(\d+) ").search(output)
  24             if mob:
  25                 w = int(mob.group(1))
  26                 h = int(mob.group(2))
  27                 self.size = w, h
  28             else:
  29                 self.size = 0, 0
  30             return self
  31         open = staticmethod(open) # For Python 2.3 compatibility
  32         def convert(self, *args, **kw):
  33             return self
  34         def copy(self):
  35             c = Image()
  36             c.path = self.path
  37             c.size = self.size
  38             return c
  39         def thumbnail(self, size, flags):
  40             os.popen("convert -resize %dx%d '%s' '%s'" % (size[0],
  41                 size[1], self.path, self.path + "TEMP"))
  42         def save(self, filename):
  43             os.popen("convert '%s' '%s'" % (self.path + "TEMP", filename))
  44             os.popen("rm '%s'" % (self.path + "TEMP"))
  45 
  46 # Specify here the default extensions and sizes. They are overridden by the wiki
  47 # config.
  48 default_thumbextensions = ["png", "jpg", "gif"]
  49 default_thumbsizes = [160, 320, 640]
  50 default_thumbname = "tmp.%(size)s.%(base)s.jpg"
  51 default_thumbignore = ["^tmp\\..*"]
  52 
  53 def execute(pagename, request):
  54     if not request.user.may.write(pagename):
  55         Page(request, pagename).send_page(request,
  56             "You need write permissions on this page to update thumbnails.")
  57 
  58     extensions = getattr(request.cfg, "thumbextensions", default_thumbextensions)
  59     thumbsizes = getattr(request.cfg, "thumbsizes", default_thumbsizes)
  60     thumbname = getattr(request.cfg, "thumbname", default_thumbname)
  61     ignore = getattr(request.cfg, "thumbignore", default_thumbignore)
  62 
  63     dir = AttachFile.getAttachDir(request, pagename)
  64     thumbs = ""
  65     try:
  66         pics = [x for x in os.listdir(dir) if [x.endswith(y) for y in extensions]]
  67         for pic in pics:
  68             # Do not make thumbnails for thumbnails.
  69             for pattern in ignore:
  70                 if re.compile(pattern).match(pic):
  71                     break
  72             else:
  73                 base, ext = os.path.splitext(pic)
  74                 im = Image.open(os.path.join(dir, pic))
  75                 for size in thumbsizes:
  76                     # Never make the thumbnail bigger than the original.
  77                     if im.size[0] <= size and im.size[1] <= size: continue
  78                     thumb = im.copy()
  79                     thumb = thumb.convert("RGB")
  80                     thumb.thumbnail((size, size), Image.ANTIALIAS)
  81                     name = thumbname % {"base" : base, "size" : size}
  82                     thumb.save(os.path.join(dir, name))
  83                     thumbs += """<a href="%s">%s</a><br />""" % (
  84                         AttachFile.getAttachUrl(pagename, name,
  85                         request).replace("do=get", "do=view"), name)
  86     except OSError:
  87         thumbs += "None<br />"
  88     Page(request, pagename).send_page(request,
  89 """
  90 These thumbnails have been updated:<br />
  91 %s
  92 Use the attachements view to delete thumbnails.
  93 """.strip() % thumbs)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2006-09-24 21:30:09, 3.4 KB) [[attachment:Thumbnails.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.