2019-01-13 20:00:12 +03:00
|
|
|
from .helper import unittest, PillowTestCase, hopper
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
from PIL import ImageStat
|
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestImageStat(PillowTestCase):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_sanity(self):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-09-05 14:03:56 +04:00
|
|
|
im = hopper()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
st = ImageStat.Stat(im)
|
|
|
|
st = ImageStat.Stat(im.histogram())
|
|
|
|
st = ImageStat.Stat(im, Image.new("1", im.size, 1))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Check these run. Exceptions will cause failures.
|
|
|
|
st.extrema
|
|
|
|
st.sum
|
|
|
|
st.mean
|
|
|
|
st.median
|
|
|
|
st.rms
|
|
|
|
st.sum2
|
|
|
|
st.var
|
|
|
|
st.stddev
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertRaises(AttributeError, lambda: st.spam)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2017-09-01 14:05:40 +03:00
|
|
|
self.assertRaises(TypeError, ImageStat.Stat, 1)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-09-05 14:03:56 +04:00
|
|
|
def test_hopper(self):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-09-05 14:03:56 +04:00
|
|
|
im = hopper()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
st = ImageStat.Stat(im)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# verify a few values
|
2014-09-14 21:00:13 +04:00
|
|
|
self.assertEqual(st.extrema[0], (0, 255))
|
|
|
|
self.assertEqual(st.median[0], 72)
|
|
|
|
self.assertEqual(st.sum[0], 1470218)
|
|
|
|
self.assertEqual(st.sum[1], 1311896)
|
|
|
|
self.assertEqual(st.sum[2], 1563008)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_constant(self):
|
|
|
|
|
|
|
|
im = Image.new("L", (128, 128), 128)
|
|
|
|
|
|
|
|
st = ImageStat.Stat(im)
|
|
|
|
|
|
|
|
self.assertEqual(st.extrema[0], (128, 128))
|
|
|
|
self.assertEqual(st.sum[0], 128**3)
|
|
|
|
self.assertEqual(st.sum2[0], 128**4)
|
|
|
|
self.assertEqual(st.mean[0], 128)
|
|
|
|
self.assertEqual(st.median[0], 128)
|
|
|
|
self.assertEqual(st.rms[0], 128)
|
|
|
|
self.assertEqual(st.var[0], 0)
|
|
|
|
self.assertEqual(st.stddev[0], 0)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|