Pillow/Tests/test_scipy.py

48 lines
1.4 KiB
Python
Raw Normal View History

2016-11-14 13:12:25 +03:00
from helper import unittest, PillowTestCase
2014-10-05 03:19:30 +04:00
try:
import numpy as np
from numpy.testing import assert_equal
from scipy import misc
HAS_SCIPY = True
2015-05-27 02:15:45 +03:00
except ImportError:
2014-10-05 03:19:30 +04:00
HAS_SCIPY = False
2015-04-24 02:26:52 +03:00
2014-10-05 03:19:30 +04:00
class Test_scipy_resize(PillowTestCase):
2015-12-25 04:02:18 +03:00
""" Tests for scipy regression in Pillow 2.6.0
2015-04-24 02:26:52 +03:00
Tests from https://github.com/scipy/scipy/blob/master/scipy/misc/pilutil.py
"""
2014-10-05 03:19:30 +04:00
def setUp(self):
if not HAS_SCIPY:
self.skipTest("Scipy Required")
def test_imresize(self):
2015-04-24 02:26:52 +03:00
im = np.random.random((10, 20))
2014-10-05 03:19:30 +04:00
for T in np.sctypes['float'] + [float]:
# 1.1 rounds to below 1.1 for float16, 1.101 works
2015-04-24 02:26:52 +03:00
im1 = misc.imresize(im, T(1.101))
self.assertEqual(im1.shape, (11, 22))
2014-10-05 03:19:30 +04:00
def test_imresize4(self):
2015-04-24 02:26:52 +03:00
im = np.array([[1, 2],
[3, 4]])
2015-07-03 09:22:56 +03:00
res = np.array([[1., 1.25, 1.75, 2.],
2015-04-24 02:26:52 +03:00
[1.5, 1.75, 2.25, 2.5],
[2.5, 2.75, 3.25, 3.5],
2015-07-03 09:22:56 +03:00
[3., 3.25, 3.75, 4.]], dtype=np.float32)
2014-10-05 03:19:30 +04:00
# Check that resizing by target size, float and int are the same
2015-04-24 02:26:52 +03:00
im2 = misc.imresize(im, (4, 4), mode='F') # output size
2014-10-05 03:19:30 +04:00
im3 = misc.imresize(im, 2., mode='F') # fraction
im4 = misc.imresize(im, 200, mode='F') # percentage
assert_equal(im2, res)
assert_equal(im3, res)
assert_equal(im4, res)
2016-11-14 13:12:25 +03:00
if __name__ == '__main__':
unittest.main()