Expose and test linear_gradient

This commit is contained in:
hugovk 2017-01-29 18:17:31 +02:00
parent 6d11fa23dd
commit 47b1c66961
4 changed files with 35 additions and 0 deletions

View File

@ -2558,3 +2558,12 @@ def effect_noise(size, sigma):
:param sigma: Standard deviation of noise. :param sigma: Standard deviation of noise.
""" """
return Image()._new(core.effect_noise(size, sigma)) return Image()._new(core.effect_noise(size, sigma))
def linear_gradient(mode):
"""
Generate 256x256 linear gradient from black to white, top to bottom.
:param mode: Input mode.
"""
return Image()._new(core.linear_gradient(mode))

Binary file not shown.

After

Width:  |  Height:  |  Size: 516 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -315,6 +315,32 @@ class TestImage(PillowTestCase):
def test_fromstring(self): def test_fromstring(self):
self.assertRaises(NotImplementedError, Image.fromstring) self.assertRaises(NotImplementedError, Image.fromstring)
def test_linear_gradient_wrong_mode(self):
# Arrange
wrong_mode = "RGB"
# Act / Assert
self.assertRaises(ValueError,
lambda: Image.linear_gradient(wrong_mode))
return
def test_linear_gradient(self):
# Arrange
for mode in ["L", "P"]:
# Act
im = Image.linear_gradient(mode)
# Assert
self.assertEqual(im.size, (256, 256))
self.assertEqual(im.mode, mode)
self.assertEqual(im.getpixel((0, 0)), 0)
self.assertEqual(im.getpixel((255, 255)), 255)
im2 = Image.open('Tests/images/linear_gradient_{}.png'.format(
mode))
self.assert_image_equal(im, im2)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()