From 53c8352f3908559cda0cde1dc52db93d05bcda8e Mon Sep 17 00:00:00 2001 From: homm Date: Wed, 15 Oct 2014 04:08:21 +0400 Subject: [PATCH] highlevel api --- PIL/ImageOps.py | 17 +++++++++++++++++ Tests/test_box_blur.py | 9 +++++++-- libImaging/BoxBlur.c | 4 ++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/PIL/ImageOps.py b/PIL/ImageOps.py index b3afd9e95..2f4cbe564 100644 --- a/PIL/ImageOps.py +++ b/PIL/ImageOps.py @@ -441,3 +441,20 @@ def unsharp_mask(im, radius=None, percent=None, threshold=None): return im.im.unsharp_mask(radius, percent, threshold) usm = unsharp_mask + + +def box_blur(image, radius): + """ + Apply box blur to given image. Box blur is operation where + each pixel becomes the average value of pixels in given radius. + Supports float radius and very large ones. Fast implementation, + works in linear time relative to the radius. + + :param image: The image to blur. + :param radius: Size of the box in one direction. Radius 0 does not blur, + radius 1 takes 1 pixel in all directions, i.e. 9 pixels in total. + :return: An image. + """ + image.load() + + return image._new(image.im.box_blur(radius)) diff --git a/Tests/test_box_blur.py b/Tests/test_box_blur.py index 87d9f354c..2e4d23dec 100644 --- a/Tests/test_box_blur.py +++ b/Tests/test_box_blur.py @@ -1,6 +1,6 @@ from helper import unittest, PillowTestCase -from PIL import Image, ImageFilter +from PIL import Image, ImageOps sample = Image.new("L", (7, 5)) @@ -14,7 +14,12 @@ sample.putdata(sum([ class TestBoxBlurApi(PillowTestCase): - pass + + def test_imageops(self): + i = ImageOps.box_blur(sample, 1) + self.assertEqual(i.mode, sample.mode) + self.assertEqual(i.size, sample.size) + self.assertIsInstance(i, Image.Image) class TestBoxBlur(PillowTestCase): diff --git a/libImaging/BoxBlur.c b/libImaging/BoxBlur.c index 2dcf0a08d..6432f888e 100644 --- a/libImaging/BoxBlur.c +++ b/libImaging/BoxBlur.c @@ -229,8 +229,8 @@ ImagingBoxBlur(Imaging im, Imaging imOut, float radius) HorizontalBoxBlur32(im, temp, radius); } - /* Blur in same direction transposed result from previout step. - Reseult will be transposes again. We'll get original image + /* Blur transposed result from previout step in same direction. + Reseult will be transposed again. We'll get original image blurred in both directions. */ if (strcmp(im->mode, "L") == 0) { HorizontalBoxBlur8(temp, imOut, radius);