Add access functions for ImagingEffectNoise with a test

This commit is contained in:
hugovk 2014-09-02 14:11:08 +03:00
parent 8b2e7ee48a
commit 860e67e1c4
3 changed files with 33 additions and 4 deletions

View File

@ -2419,3 +2419,19 @@ def _show(image, **options):
def _showxv(image, title=None, **options): def _showxv(image, title=None, **options):
from PIL import ImageShow from PIL import ImageShow
ImageShow.show(image, title, **options) 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

View File

@ -140,6 +140,19 @@ class TestImage(PillowTestCase):
img_colors = sorted(img.getcolors()) img_colors = sorted(img.getcolors())
self.assertEqual(img_colors, expected_colors) 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__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -74,7 +74,7 @@ ImagingEffectMandelbrot(int xsize, int ysize, double extent[4], int quality)
Imaging Imaging
ImagingEffectNoise(int xsize, int ysize, float sigma) ImagingEffectNoise(int xsize, int ysize, float sigma)
{ {
/* Generate gaussian noise centered around 128 */ /* Generate Gaussian noise centered around 128 */
Imaging imOut; Imaging imOut;
int x, y; int x, y;
@ -83,19 +83,19 @@ ImagingEffectNoise(int xsize, int ysize, float sigma)
imOut = ImagingNew("L", xsize, ysize); imOut = ImagingNew("L", xsize, ysize);
if (!imOut) if (!imOut)
return NULL; return NULL;
next = 0.0; next = 0.0;
nextok = 0; nextok = 0;
for (y = 0; y < imOut->ysize; y++) { for (y = 0; y < imOut->ysize; y++) {
UINT8* out = imOut->image8[y]; UINT8* out = imOut->image8[y];
for (x = 0; x < imOut->xsize; x++) { for (x = 0; x < imOut->xsize; x++) {
if (nextok) { if (nextok) {
this = next; this = next;
nextok = 0; nextok = 0;
} else { } else {
/* after numerical recepies */ /* after numerical recipes */
double v1, v2, radius, factor; double v1, v2, radius, factor;
do { do {
v1 = rand()*(2.0/32767.0) - 1.0; v1 = rand()*(2.0/32767.0) - 1.0;