# Phatch - Photo Batch Processor
# Copyright (C) 2007-2008 www.stani.be
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/
#
# Phatch recommends SPE (http://pythonide.stani.be) for editing python files.

# Embedded icon is taken from www.openclipart.org (public domain)

# Follows PEP8

from core import models
from lib.reverse_translation import _t

#---PIL


def init():
    global Image, ImageFilter, imtools
    import Image
    import ImageFilter
    from lib import imtools


def median(image, radius, amount=100):
    """Apply a filter
    - amount: 0-1"""
    image = imtools.convert_safe_mode(image)
    medianed = image.filter(ImageFilter.MedianFilter(radius))
    if amount < 100:
        return imtools.blend(image, medianed, amount / 100.0)
    return medianed

#---Phatch


class Action(models.Action):
    label = _t('Median')
    author = 'Stani'
    email = 'spe.stani.be@gmail.com'
    init = staticmethod(init)
    pil = staticmethod(median)
    version = '0.1'
    tags = [_t('filter')]
    __doc__ = _t("Copies the median pixel value")

    def interface(self, fields):
        fields[_t('Radius')] = self.RankSizeField(self.RANK_SIZES[0])
        fields[_t('Amount')] = self.SliderField(100, 1, 100)

    icon = \
'x\xda\x01 \x07\xdf\xf8\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x000\x00\
\x00\x00+\x08\x06\x00\x00\x00>\x13\x0b\xdf\x00\x00\x00\x04sBIT\x08\x08\x08\
\x08|\x08d\x88\x00\x00\x06\xd7IDATh\x81\xcd\x99mL\x1c\xc7\x19\xc7\xff\xcf\
\xee\xde-\xc7qp\x1c\xe2\x8c!p$\x98\x82\xf0Q\x1b\xda\x80l\xe8\xcbEu,\x1bL%\
\x1a\xdb\xaa\xab(\x96]\xa9M>Dr"\xa7\x8d\x9a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6k\xe0+\xc7\
@\xec\x13\x91\x1c+f\x00\xb2la\xcfO(\x93\x1d\x9bI\xdb=\xafv\xf9\xc8\xacY}\xe3\
W\x87\xbb\xef\x06\x86\xbeWe>y_\xb9w\xa7`)\xe0\xd6\xe7\xc1\xe6E\x160\xf9\xaf8\
Ff\xf1\xde\x83y\xad\n\xc0\x10\x80E"\xd2\x00,\x02\xd0\x00,2\xc6\xd6\x14\xec_y\
\t\xadb\xc0\x1c?6\x99 G"0\xc5\xeaL\x00DI\x82\xb4\xb4\x04\xd6\xec\xc9q\\\xf8O\
`\n@d\xcfws\xac\x17?\rL\xc7\x06\x9b4\xf0\xf8\xf1\x86\x19X\xc5\x90\x18\x1b\
\xac\x14+b\xac\x08x\xb84\t\xcb_\x0c\x18\x96o\xabz\xec\xefR\xacD\xe2w*\xa3\
\xf0\x7f;\xfd\x7f\xe6\xbf\x82\x1co\xb0\n\xf7\xb5u\x00\x00\x00\x00IEND\xaeB`\
\x82\xbea_\x9e'
