From 860e67e1c455d4d5ef8c0f8ab366e1840437df3c Mon Sep 17 00:00:00 2001 From: hugovk Date: Tue, 2 Sep 2014 14:11:08 +0300 Subject: [PATCH] Add access functions for ImagingEffectNoise with a test --- PIL/Image.py | 16 ++++++++++++++++ Tests/test_image.py | 13 +++++++++++++ libImaging/Effects.c | 8 ++++---- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 480410eff..605595912 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -2419,3 +2419,19 @@ def _show(image, **options): def _showxv(image, title=None, **options): from PIL import ImageShow ImageShow.show(image, title, **options) + + +# -------------------------------------------------------------------- +# Effects + +def effect_noise(size, sigma): + """ + Generate Gaussian noise centered around 128 + + :param size: The requested size in pixels, as a 2-tuple: + (width, height). + :param sigma: Standard deviation of noise. + """ + return Image()._new(core.effect_noise(size, sigma)) + +# End of file diff --git a/Tests/test_image.py b/Tests/test_image.py index cd46c9713..bbc9a43fc 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -140,6 +140,19 @@ class TestImage(PillowTestCase): img_colors = sorted(img.getcolors()) self.assertEqual(img_colors, expected_colors) + def test_noise(self): + # Arrange + size = (100, 100) + sigma = 128 + + # Act + im = Image.effect_noise(size, sigma) + + # Assert + self.assertEqual(im.size, (100, 100)) + self.assertEqual(im.getpixel((0, 0)), 60) + self.assertEqual(im.getpixel((0, 1)), 28) + if __name__ == '__main__': unittest.main() diff --git a/libImaging/Effects.c b/libImaging/Effects.c index db6e72989..30e44d570 100644 --- a/libImaging/Effects.c +++ b/libImaging/Effects.c @@ -74,7 +74,7 @@ ImagingEffectMandelbrot(int xsize, int ysize, double extent[4], int quality) Imaging ImagingEffectNoise(int xsize, int ysize, float sigma) { - /* Generate gaussian noise centered around 128 */ + /* Generate Gaussian noise centered around 128 */ Imaging imOut; int x, y; @@ -83,19 +83,19 @@ ImagingEffectNoise(int xsize, int ysize, float sigma) imOut = ImagingNew("L", xsize, ysize); if (!imOut) - return NULL; + return NULL; next = 0.0; nextok = 0; for (y = 0; y < imOut->ysize; y++) { UINT8* out = imOut->image8[y]; - for (x = 0; x < imOut->xsize; x++) { + for (x = 0; x < imOut->xsize; x++) { if (nextok) { this = next; nextok = 0; } else { - /* after numerical recepies */ + /* after numerical recipes */ double v1, v2, radius, factor; do { v1 = rand()*(2.0/32767.0) - 1.0;