Add access functions for ImagingEffectSpread with a test

This commit is contained in:
hugovk 2014-09-02 15:53:58 +03:00
parent 8c96d38ce9
commit bcc5305125
5 changed files with 30 additions and 6 deletions

View File

@ -1910,6 +1910,16 @@ class Image:
im = self.im.transpose(method)
return self._new(im)
def effect_spread(self, distance):
"""
Randomly spread pixels in an image.
:param distance: Distance to spread pixels.
"""
self.load()
im = self.im.effect_spread(distance)
return self._new(im)
# --------------------------------------------------------------------
# Lazy operations
@ -2436,6 +2446,7 @@ def effect_mandelbrot(size, extent, quality):
"""
return Image()._new(core.effect_mandelbrot(size, extent, quality))
def effect_noise(size, sigma):
"""
Generate Gaussian noise centered around 128.

View File

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

View File

@ -152,7 +152,7 @@ class TestImage(PillowTestCase):
# Assert
self.assertEqual(im.size, (512, 512))
im2 = Image.open('Tests/images/mandelbrot.png')
im2 = Image.open('Tests/images/effect_mandelbrot.png')
self.assert_image_equal(im, im2)
@unittest.skipUnless(sys.platform.startswith('win32'),
@ -170,6 +170,19 @@ class TestImage(PillowTestCase):
self.assertEqual(im.getpixel((0, 0)), 60)
self.assertEqual(im.getpixel((0, 1)), 28)
def test_effect_spread(self):
# Arrange
im = lena()
distance = 10
# Act
im2 = im.effect_spread(distance)
# Assert
self.assertEqual(im.size, (128, 128))
im3 = Image.open('Tests/images/effect_spread.png')
self.assert_image_equal(im2, im3)
if __name__ == '__main__':
unittest.main()

View File

@ -124,11 +124,11 @@ ImagingEffectSpread(Imaging imIn, int distance)
imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize);
if (!imOut)
return NULL;
return NULL;
#define SPREAD(type, image)\
#define SPREAD(type, image)\
for (y = 0; y < imIn->ysize; y++)\
for (x = 0; x < imIn->xsize; x++) {\
for (x = 0; x < imIn->xsize; x++) {\
int xx = x + (rand() % distance) - distance/2;\
int yy = y + (rand() % distance) - distance/2;\
if (xx >= 0 && xx < imIn->xsize && yy >= 0 && yy < imIn->ysize) {\
@ -139,9 +139,9 @@ ImagingEffectSpread(Imaging imIn, int distance)
}
if (imIn->image8) {
SPREAD(UINT8, image8);
SPREAD(UINT8, image8);
} else {
SPREAD(INT32, image32);
SPREAD(INT32, image32);
}
ImagingCopyInfo(imOut, imIn);