Attachment 'GreyColor.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3    MoinMoin - Color2Grey Image Correction Effect
   4 
   5    Color2Grey image correction algorithm implemented according to
   6    http://www.e56.de/c2g.php, copyrights by Martin Faust
   7 
   8    Please note:
   9    Color2Grey Image Correction Effect needs
  10    * Python Image Library (PIL) from http://www.pythonware.com/products/pil/
  11    * NumPy from http://numpy.scipy.org/
  12 
  13    @copyright: 2008 by Oliver Siemoneit
  14                2008 refactored to an action for all jpeg images of a page by MoinMoin:ReimarBauer
  15    @license: GNU GPL, see COPYING for details.
  16 """
  17 
  18 HELPERS = True
  19 try:
  20     from PIL import Image
  21     import numpy
  22 except ImportError:
  23     HELPERS = False
  24 
  25 import colorsys, os, StringIO
  26 from MoinMoin.Page import Page
  27 from MoinMoin.action import AttachFile, cache
  28 
  29 action_name = __name__.split('.')[-1]
  30 
  31 def process_files(pagename, request, extension):
  32     files = AttachFile._get_files(request, pagename)
  33     attach_dir = AttachFile.getAttachDir(request, pagename)
  34     lightness_correction = True # Set lightness_correction by default to "True"!
  35     msg = "nothing done"
  36     state = "info"
  37     for attfile in files:
  38         if attfile.lower().endswith(extension):
  39             key = cache.key(request, itemname=pagename, attachname=attfile)
  40             # ToDo naming scheme for prefix wanted
  41             greyimage = 'G' + key
  42             if not cache.exists(request, greyimage):
  43                 im = Image.open(os.path.join(attach_dir, attfile))
  44                 if not im.mode in ('1', 'L'): # Don't process black/white or greyscale images
  45                     old_size = im.size
  46                     im.thumbnail((640, 640), Image.ANTIALIAS) # For the sake of speed: process thumbnail only
  47                     im = im.convert('RGB')
  48                     pix = im.load() # Load the image in memory so we don't need getpixel and putpixel
  49 
  50                     grey = numpy.zeros(im.size, float)
  51                     minimum = 100.0
  52                     maximum = -100.0
  53                     total = 0.0
  54 
  55                     for y_pos in range(im.size[1]):
  56                         for x_pos in range(im.size[0]):
  57                             red, green, blue = pix[x_pos, y_pos]
  58                             hue, saturation, lightness = colorsys.rgb_to_hsv(red, green, blue)
  59                             hue = hue * 360.0 # Algorithm expects value to be within <0;360>
  60                             if saturation == 0.0:
  61                                 grey[x_pos][y_pos] = 1.5 * lightness
  62                             else:
  63                                 grey[x_pos][y_pos] = lightness + lightness * saturation
  64                                 minimum = min((grey[x_pos][y_pos], minimum))
  65                                 maximum = max((grey[x_pos][y_pos], maximum))
  66                             total += grey[x_pos][y_pos]
  67 
  68                     mean = total / (float)(im.size[1] * im.size[0])
  69                     minimum = 0.0
  70                     maximum = (mean + maximum) * 0.5
  71                     for y_pos in range(im.size[1]):
  72                         for x_pos in range(im.size[0]):
  73                             if lightness_correction:
  74                                 lightness = 0.9 * 255.0 * (grey[x_pos][y_pos] - minimum) / (maximum - minimum)
  75                             else:
  76                                 lightness = 255.0 * (grey[x_pos][y_pos] - minimum) / (maximum - minimum)
  77                                 lightness = max((min((lightness, 255)), 0.0))
  78                             pix[x_pos, y_pos] = (int(lightness), int(lightness), int(lightness))
  79 
  80                     buf = StringIO.StringIO()
  81                     im = im.resize(old_size, Image.ANTIALIAS)
  82                     im.save(buf, 'JPEG')
  83                     buf.flush()
  84                     buf.seek(0)
  85                     cache.put(request, greyimage, buf, filename=attfile)
  86                     buf.close()
  87                     state = "info"
  88                     msg = "file(s) processed"
  89 
  90     return msg, state
  91 
  92 def execute(pagename, request):
  93     """ dispatcher for grey color conversion action. """
  94     _ = request.getText
  95     # XXX Why is this action restricted to jpg / jpeg? What about gif, png? Why restrict it at all?
  96     extension = '.jpg'
  97     msg = None
  98     if not HELPERS:
  99         msg = _('The action %(action)s needs Python Imaging Library (PIL) and numpy installed') % {'action': action_name}
 100         state = "error"
 101     if not msg:
 102         msg, state = process_files(pagename, request, extension)
 103     if msg:
 104         request.theme.add_msg(msg, state)
 105         Page(request, pagename).send_page()
 106     return

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] (2008-09-19 20:47:51, 16.9 KB) [[attachment:C2GExtensionModule.zip]]
  • [get | view] (2008-09-19 20:48:29, 17.6 KB) [[attachment:DaltonizeExtensionModule.zip]]
  • [get | view] (2008-09-07 22:34:29, 4.5 KB) [[attachment:GreyColor.py]]
 All files | Selected Files: delete move to page copy to page

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