Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast filters #2679

Merged
merged 21 commits into from
Sep 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,8 @@ def filter(self, filter):
:param filter: Filter kernel.
:returns: An :py:class:`~PIL.Image.Image` object. """

from . import ImageFilter

self.load()

if isinstance(filter, collections.Callable):
Expand All @@ -1113,9 +1115,10 @@ def filter(self, filter):
raise TypeError("filter argument should be ImageFilter.Filter " +
"instance or class")

if self.im.bands == 1:
multiband = isinstance(filter, ImageFilter.MultibandFilter)
if self.im.bands == 1 or multiband:
return self._new(filter.filter(self.im))
# fix to handle multiband images since _imaging doesn't

ims = []
for c in range(self.im.bands):
ims.append(self._new(filter.filter(self.im.getband(c))))
Expand Down
10 changes: 7 additions & 3 deletions PIL/ImageFilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ class Filter(object):
pass


class Kernel(Filter):
class MultibandFilter(Filter):
pass


class Kernel(MultibandFilter):
"""
Create a convolution kernel. The current version only
supports 3x3 and 5x5 integer and floating point kernels.
Expand Down Expand Up @@ -142,7 +146,7 @@ def filter(self, image):
return image.modefilter(self.size)


class GaussianBlur(Filter):
class GaussianBlur(MultibandFilter):
"""Gaussian blur filter.

:param radius: Blur radius.
Expand All @@ -156,7 +160,7 @@ def filter(self, image):
return image.gaussian_blur(self.radius)


class UnsharpMask(Filter):
class UnsharpMask(MultibandFilter):
"""Unsharp mask filter.

See Wikipedia's entry on `digital unsharp masking`_ for an explanation of
Expand Down
48 changes: 30 additions & 18 deletions Tests/test_image_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,38 @@ def test_rankfilter_properties(self):
self.assertEqual(rankfilter.rank, 2)

def test_consistency_3x3(self):
im = Image.open("Tests/images/hopper.bmp")
emboss = im.filter(ImageFilter.Kernel((3, 3),
(-1, -1, 0,
-1, 0, 1,
0, 1, 1), .3))

self.assert_image_equal(
emboss, Image.open("Tests/images/hopper_emboss.bmp"))
source = Image.open("Tests/images/hopper.bmp")
reference = Image.open("Tests/images/hopper_emboss.bmp")
kernel = ImageFilter.Kernel((3, 3),
(-1, -1, 0,
-1, 0, 1,
0, 1, 1), .3)
source = source.split() * 2
reference = reference.split() * 2

for mode in ['L', 'LA', 'RGB', 'CMYK']:
self.assert_image_equal(
Image.merge(mode, source[:len(mode)]).filter(kernel),
Image.merge(mode, reference[:len(mode)]),
)

def test_consistency_5x5(self):
im = Image.open("Tests/images/hopper.bmp")
emboss = im.filter(ImageFilter.Kernel((5, 5),
(-1, -1, -1, -1, 0,
-1, -1, -1, 0, 1,
-1, -1, 0, 1, 1,
-1, 0, 1, 1, 1,
0, 1, 1, 1, 1), 0.3))

self.assert_image_equal(
emboss, Image.open("Tests/images/hopper_emboss_more.bmp"))
source = Image.open("Tests/images/hopper.bmp")
reference = Image.open("Tests/images/hopper_emboss_more.bmp")
kernel = ImageFilter.Kernel((5, 5),
(-1, -1, -1, -1, 0,
-1, -1, -1, 0, 1,
-1, -1, 0, 1, 1,
-1, 0, 1, 1, 1,
0, 1, 1, 1, 1), 0.3)
source = source.split() * 2
reference = reference.split() * 2

for mode in ['L', 'LA', 'RGB', 'CMYK']:
self.assert_image_equal(
Image.merge(mode, source[:len(mode)]).filter(kernel),
Image.merge(mode, reference[:len(mode)]),
)


if __name__ == '__main__':
Expand Down
8 changes: 6 additions & 2 deletions _imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ _filter(ImagingObject* self, PyObject* args)
Py_ssize_t kernelsize;
FLOAT32* kerneldata;

int xsize, ysize;
int xsize, ysize, i;
float divisor, offset;
PyObject* kernel = NULL;
if (!PyArg_ParseTuple(args, "(ii)ffO", &xsize, &ysize,
Expand All @@ -835,8 +835,12 @@ _filter(ImagingObject* self, PyObject* args)
return ImagingError_ValueError("bad kernel size");
}

for (i = 0; i < kernelsize; ++i) {
kerneldata[i] /= divisor;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precompute by dividing coefficients to divisor rather than each result pixel.

}

imOut = PyImagingNew(
ImagingFilter(self->image, xsize, ysize, kerneldata, offset, divisor)
ImagingFilter(self->image, xsize, ysize, kerneldata, offset)
);

free(kerneldata);
Expand Down
Loading