diff --git a/PIL/Image.py b/PIL/Image.py index 605595912..4c9a77e79 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -2424,9 +2424,21 @@ def _showxv(image, title=None, **options): # -------------------------------------------------------------------- # Effects +def effect_mandelbrot(size, extent, quality): + """ + Generate a Mandelbrot set covering the given extent. + + :param size: The requested size in pixels, as a 2-tuple: + (width, height). + :param extent: The extent to cover, as a 4-tuple: + (x0, y0, x1, y2). + :param quality: Quality. + """ + return Image()._new(core.effect_mandelbrot(size, extent, quality)) + def effect_noise(size, sigma): """ - Generate Gaussian noise centered around 128 + Generate Gaussian noise centered around 128. :param size: The requested size in pixels, as a 2-tuple: (width, height). diff --git a/Tests/images/mandelbrot.png b/Tests/images/mandelbrot.png new file mode 100644 index 000000000..d8167e721 Binary files /dev/null and b/Tests/images/mandelbrot.png differ diff --git a/Tests/test_image.py b/Tests/test_image.py index 83d5b0d1f..77baf4fe7 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -141,6 +141,20 @@ class TestImage(PillowTestCase): img_colors = sorted(img.getcolors()) self.assertEqual(img_colors, expected_colors) + def test_effect_mandelbrot(self): + # Arrange + size = (512, 512) + extent = (-3, -2.5, 2, 2.5) + quality = 100 + + # Act + im = Image.effect_mandelbrot(size, extent, quality) + + # Assert + self.assertEqual(im.size, (512, 512)) + im2 = Image.open('Tests/images/mandelbrot.png') + self.assert_image_equal(im, im2) + @unittest.skipUnless(sys.platform.startswith('win32'), "Stalls on Travis CI, passes on Windows") def test_effect_noise(self): @@ -156,7 +170,6 @@ class TestImage(PillowTestCase): 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 90e0fff30..2ebd9f880 100644 --- a/libImaging/Effects.c +++ b/libImaging/Effects.c @@ -48,25 +48,25 @@ ImagingEffectMandelbrot(int xsize, int ysize, double extent[4], int quality) for (y = 0; y < ysize; y++) { UINT8* buf = im->image8[y]; - for (x = 0; x < xsize; x++) { - x1 = y1 = xi2 = yi2 = 0.0; - cr = x*dr + extent[0]; - ci = y*di + extent[1]; - for (k = 1;; k++) { - y1 = 2*x1*y1 + ci; - x1 = xi2 - yi2 + cr; - xi2 = x1*x1; - yi2 = y1*y1; - if ((xi2 + yi2) > radius) { - buf[x] = k*255/quality; - break; - } - if (k > quality) { - buf[x] = 0; - break; - } - } - } + for (x = 0; x < xsize; x++) { + x1 = y1 = xi2 = yi2 = 0.0; + cr = x*dr + extent[0]; + ci = y*di + extent[1]; + for (k = 1;; k++) { + y1 = 2*x1*y1 + ci; + x1 = xi2 - yi2 + cr; + xi2 = x1*x1; + yi2 = y1*y1; + if ((xi2 + yi2) > radius) { + buf[x] = k*255/quality; + break; + } + if (k > quality) { + buf[x] = 0; + break; + } + } + } } return im; }