2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# standard filters
|
|
|
|
#
|
|
|
|
# History:
|
|
|
|
# 1995-11-27 fl Created
|
|
|
|
# 2002-06-08 fl Added rank and mode filters
|
|
|
|
# 2003-09-15 fl Fixed rank calculation in rank filter; added expand call
|
|
|
|
#
|
|
|
|
# Copyright (c) 1997-2003 by Secret Labs AB.
|
|
|
|
# Copyright (c) 1995-2002 by Fredrik Lundh.
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
2012-10-16 06:15:25 +04:00
|
|
|
from functools import reduce
|
|
|
|
|
2013-10-13 03:49:32 +04:00
|
|
|
|
2013-03-27 20:44:28 +04:00
|
|
|
class Filter(object):
|
2010-07-31 06:52:47 +04:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Kernel(Filter):
|
2013-10-13 03:49:32 +04:00
|
|
|
"""
|
|
|
|
Create a convolution kernel. The current version only
|
|
|
|
supports 3x3 and 5x5 integer and floating point kernels.
|
|
|
|
|
|
|
|
In the current version, kernels can only be applied to
|
|
|
|
"L" and "RGB" images.
|
|
|
|
|
|
|
|
:param size: Kernel size, given as (width, height). In the current
|
|
|
|
version, this must be (3,3) or (5,5).
|
|
|
|
:param kernel: A sequence containing kernel weights.
|
|
|
|
:param scale: Scale factor. If given, the result for each pixel is
|
|
|
|
divided by this value. the default is the sum of the
|
|
|
|
kernel weights.
|
|
|
|
:param offset: Offset. If given, this value is added to the result,
|
|
|
|
after it has been divided by the scale factor.
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def __init__(self, size, kernel, scale=None, offset=0):
|
|
|
|
if scale is None:
|
|
|
|
# default scale is sum of kernel
|
2014-08-26 17:47:10 +04:00
|
|
|
scale = reduce(lambda a, b: a+b, kernel)
|
2010-07-31 06:52:47 +04:00
|
|
|
if size[0] * size[1] != len(kernel):
|
|
|
|
raise ValueError("not enough coefficients in kernel")
|
|
|
|
self.filterargs = size, scale, offset, kernel
|
|
|
|
|
|
|
|
def filter(self, image):
|
|
|
|
if image.mode == "P":
|
|
|
|
raise ValueError("cannot filter palette images")
|
2012-10-21 01:05:13 +04:00
|
|
|
return image.filter(*self.filterargs)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-10-13 03:49:32 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
class BuiltinFilter(Kernel):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class RankFilter(Filter):
|
2013-10-13 03:49:32 +04:00
|
|
|
"""
|
|
|
|
Create a rank filter. The rank filter sorts all pixels in
|
|
|
|
a window of the given size, and returns the **rank**'th value.
|
|
|
|
|
|
|
|
:param size: The kernel size, in pixels.
|
|
|
|
:param rank: What pixel value to pick. Use 0 for a min filter,
|
|
|
|
``size * size / 2`` for a median filter, ``size * size - 1``
|
|
|
|
for a max filter, etc.
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
name = "Rank"
|
|
|
|
|
|
|
|
def __init__(self, size, rank):
|
|
|
|
self.size = size
|
|
|
|
self.rank = rank
|
|
|
|
|
|
|
|
def filter(self, image):
|
|
|
|
if image.mode == "P":
|
|
|
|
raise ValueError("cannot filter palette images")
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
image = image.expand(self.size//2, self.size//2)
|
2010-07-31 06:52:47 +04:00
|
|
|
return image.rankfilter(self.size, self.rank)
|
|
|
|
|
|
|
|
|
|
|
|
class MedianFilter(RankFilter):
|
2013-10-13 03:49:32 +04:00
|
|
|
"""
|
|
|
|
Create a median filter. Picks the median pixel value in a window with the
|
|
|
|
given size.
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-10-13 03:49:32 +04:00
|
|
|
:param size: The kernel size, in pixels.
|
|
|
|
"""
|
|
|
|
name = "Median"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def __init__(self, size=3):
|
|
|
|
self.size = size
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
self.rank = size*size//2
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
class MinFilter(RankFilter):
|
2013-10-13 03:49:32 +04:00
|
|
|
"""
|
|
|
|
Create a min filter. Picks the lowest pixel value in a window with the
|
|
|
|
given size.
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-10-13 03:49:32 +04:00
|
|
|
:param size: The kernel size, in pixels.
|
|
|
|
"""
|
|
|
|
name = "Min"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def __init__(self, size=3):
|
|
|
|
self.size = size
|
|
|
|
self.rank = 0
|
|
|
|
|
|
|
|
|
|
|
|
class MaxFilter(RankFilter):
|
2013-10-13 03:49:32 +04:00
|
|
|
"""
|
|
|
|
Create a max filter. Picks the largest pixel value in a window with the
|
|
|
|
given size.
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-10-13 03:49:32 +04:00
|
|
|
:param size: The kernel size, in pixels.
|
|
|
|
"""
|
|
|
|
name = "Max"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def __init__(self, size=3):
|
|
|
|
self.size = size
|
|
|
|
self.rank = size*size-1
|
|
|
|
|
|
|
|
|
|
|
|
class ModeFilter(Filter):
|
2013-10-13 03:49:32 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
Create a mode filter. Picks the most frequent pixel value in a box with the
|
|
|
|
given size. Pixel values that occur only once or twice are ignored; if no
|
|
|
|
pixel value occurs more than twice, the original pixel value is preserved.
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-10-13 03:49:32 +04:00
|
|
|
:param size: The kernel size, in pixels.
|
|
|
|
"""
|
|
|
|
name = "Mode"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def __init__(self, size=3):
|
|
|
|
self.size = size
|
2013-10-13 03:49:32 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def filter(self, image):
|
|
|
|
return image.modefilter(self.size)
|
|
|
|
|
|
|
|
|
|
|
|
class GaussianBlur(Filter):
|
2013-10-13 03:49:32 +04:00
|
|
|
"""Gaussian blur filter.
|
|
|
|
|
|
|
|
:param radius: Blur radius.
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
name = "GaussianBlur"
|
|
|
|
|
|
|
|
def __init__(self, radius=2):
|
2012-12-07 04:19:55 +04:00
|
|
|
self.radius = radius
|
2013-10-13 03:49:32 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def filter(self, image):
|
|
|
|
return image.gaussian_blur(self.radius)
|
|
|
|
|
|
|
|
|
|
|
|
class UnsharpMask(Filter):
|
2013-10-13 03:49:32 +04:00
|
|
|
"""Unsharp mask filter.
|
|
|
|
|
|
|
|
See Wikipedia's entry on `digital unsharp masking`_ for an explanation of
|
|
|
|
the parameters.
|
|
|
|
|
2014-08-28 15:44:19 +04:00
|
|
|
.. _digital unsharp masking:
|
|
|
|
https://en.wikipedia.org/wiki/Unsharp_masking#Digital_unsharp_masking
|
2013-10-13 03:49:32 +04:00
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
name = "UnsharpMask"
|
|
|
|
|
|
|
|
def __init__(self, radius=2, percent=150, threshold=3):
|
2012-12-07 04:19:55 +04:00
|
|
|
self.radius = radius
|
2010-07-31 06:52:47 +04:00
|
|
|
self.percent = percent
|
|
|
|
self.threshold = threshold
|
2013-10-13 03:49:32 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def filter(self, image):
|
|
|
|
return image.unsharp_mask(self.radius, self.percent, self.threshold)
|
|
|
|
|
|
|
|
|
|
|
|
class BLUR(BuiltinFilter):
|
|
|
|
name = "Blur"
|
|
|
|
filterargs = (5, 5), 16, 0, (
|
|
|
|
1, 1, 1, 1, 1,
|
|
|
|
1, 0, 0, 0, 1,
|
|
|
|
1, 0, 0, 0, 1,
|
|
|
|
1, 0, 0, 0, 1,
|
|
|
|
1, 1, 1, 1, 1
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CONTOUR(BuiltinFilter):
|
|
|
|
name = "Contour"
|
|
|
|
filterargs = (3, 3), 1, 255, (
|
|
|
|
-1, -1, -1,
|
|
|
|
-1, 8, -1,
|
|
|
|
-1, -1, -1
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class DETAIL(BuiltinFilter):
|
|
|
|
name = "Detail"
|
|
|
|
filterargs = (3, 3), 6, 0, (
|
|
|
|
0, -1, 0,
|
|
|
|
-1, 10, -1,
|
|
|
|
0, -1, 0
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class EDGE_ENHANCE(BuiltinFilter):
|
|
|
|
name = "Edge-enhance"
|
|
|
|
filterargs = (3, 3), 2, 0, (
|
|
|
|
-1, -1, -1,
|
|
|
|
-1, 10, -1,
|
|
|
|
-1, -1, -1
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class EDGE_ENHANCE_MORE(BuiltinFilter):
|
|
|
|
name = "Edge-enhance More"
|
|
|
|
filterargs = (3, 3), 1, 0, (
|
|
|
|
-1, -1, -1,
|
|
|
|
-1, 9, -1,
|
|
|
|
-1, -1, -1
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class EMBOSS(BuiltinFilter):
|
|
|
|
name = "Emboss"
|
|
|
|
filterargs = (3, 3), 1, 128, (
|
|
|
|
-1, 0, 0,
|
|
|
|
0, 1, 0,
|
|
|
|
0, 0, 0
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class FIND_EDGES(BuiltinFilter):
|
|
|
|
name = "Find Edges"
|
|
|
|
filterargs = (3, 3), 1, 0, (
|
|
|
|
-1, -1, -1,
|
|
|
|
-1, 8, -1,
|
|
|
|
-1, -1, -1
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class SMOOTH(BuiltinFilter):
|
|
|
|
name = "Smooth"
|
|
|
|
filterargs = (3, 3), 13, 0, (
|
|
|
|
1, 1, 1,
|
|
|
|
1, 5, 1,
|
|
|
|
1, 1, 1
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class SMOOTH_MORE(BuiltinFilter):
|
|
|
|
name = "Smooth More"
|
|
|
|
filterargs = (5, 5), 100, 0, (
|
|
|
|
1, 1, 1, 1, 1,
|
|
|
|
1, 5, 5, 5, 1,
|
|
|
|
1, 5, 44, 5, 1,
|
|
|
|
1, 5, 5, 5, 1,
|
|
|
|
1, 1, 1, 1, 1
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class SHARPEN(BuiltinFilter):
|
|
|
|
name = "Sharpen"
|
|
|
|
filterargs = (3, 3), 16, 0, (
|
|
|
|
-2, -2, -2,
|
|
|
|
-2, 32, -2,
|
|
|
|
-2, -2, -2
|
|
|
|
)
|