From 2a1a371e5cc538ae6d1db71898598f8e8fff19ee Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 29 Jan 2015 09:29:10 +0000 Subject: [PATCH] started test_resample.py --- TODO | 2 ++ test/Makefile.am | 3 +- test/test_cli.sh | 3 ++ test/test_histogram.py | 3 +- test/test_python.sh | 2 ++ test/test_resample.py | 79 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 2 deletions(-) create mode 100755 test/test_resample.py diff --git a/TODO b/TODO index c4915dfc..7ace6a55 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,5 @@ +- similarity problems? see test_resample + - add imageize from 2D arrays, eg. we should be able to write im = im.erode([[128, 255, 128], diff --git a/test/Makefile.am b/test/Makefile.am index 4992e87e..af987e1d 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -15,7 +15,8 @@ EXTRA_DIST = \ test_create.py \ test_draw.py \ test_histogram.py \ - test_morphology.py + test_morphology.py \ + test_resample.py # don't run test_thumbnail.sh by default, it takes ages diff --git a/test/test_cli.sh b/test/test_cli.sh index 421ffdf9..7e10e77d 100755 --- a/test/test_cli.sh +++ b/test/test_cli.sh @@ -1,5 +1,8 @@ #!/bin/sh +# this test is also in test_resample.py (though much smaller and neater) ... +# keep this test to exercise the cli interface + set -x . ./variables.sh diff --git a/test/test_histogram.py b/test/test_histogram.py index 6a60c0b7..2b389b7b 100755 --- a/test/test_histogram.py +++ b/test/test_histogram.py @@ -118,7 +118,8 @@ class TestHistogram(unittest.TestCase): self.assertEqual(im.width, im2.width) self.assertEqual(im.height, im2.height) - self.assertTrue(im.avg() < im2.avg()) + # new mean should be closer to target mean + self.assertTrue(abs(im.avg() - 128) > abs(im2.avg() - 128)) if __name__ == '__main__': unittest.main() diff --git a/test/test_python.sh b/test/test_python.sh index 7d45aa66..1427dd7a 100755 --- a/test/test_python.sh +++ b/test/test_python.sh @@ -18,6 +18,7 @@ python2 test_create.py python2 test_draw.py python2 test_histogram.py python2 test_morphology.py +python2 test_resample.py echo "testing with python3 ..." @@ -29,3 +30,4 @@ python3 test_create.py python3 test_draw.py python3 test_histogram.py python3 test_morphology.py +python3 test_resample.py diff --git a/test/test_resample.py b/test/test_resample.py new file mode 100755 index 00000000..5e657a95 --- /dev/null +++ b/test/test_resample.py @@ -0,0 +1,79 @@ +#!/usr/bin/python + +import unittest +import math + +#import logging +#logging.basicConfig(level = logging.DEBUG) + +from gi.repository import Vips + +# an expanding zip ... if either of the args is a scalar or a one-element list, +# duplicate it down the other side +def zip_expand(x, y): + # handle singleton list case + if isinstance(x, list) and len(x) == 1: + x = x[0] + if isinstance(y, list) and len(y) == 1: + y = y[0] + + if isinstance(x, list) and isinstance(y, list): + return list(zip(x, y)) + elif isinstance(x, list): + return [[i, y] for i in x] + elif isinstance(y, list): + return [[x, j] for j in y] + else: + return [[x, y]] + +class TestResample(unittest.TestCase): + # test a pair of things which can be lists for approx. equality + def assertAlmostEqualObjects(self, a, b, places = 4, msg = ''): + # print 'assertAlmostEqualObjects %s = %s' % (a, b) + for x, y in zip_expand(a, b): + self.assertAlmostEqual(x, y, places = places, msg = msg) + + def test_affine(self): + im = Vips.Image.new_from_file("images/IMG_4618.jpg") + + # vsqbs is non-interpolatory, don't test this way + for name in ["nearest", "bicubic", "bilinear", "nohalo", "lbb"]: + x = im + interpolate = Vips.Interpolate.new(name) + for i in range(4): + # 90 degree rotate + x = x.affine([0, -1, 1, 0], interpolate = interpolate) + + self.assertEqual((x - im).abs().max(), 0) + + def test_resize(self): + im = Vips.Image.new_from_file("images/IMG_4618.jpg") + im2 = im.resize(0.25) + self.assertEqual(im2.width, im.width // 4) + self.assertEqual(im2.height, im.height // 4) + + def test_shrink(self): + im = Vips.Image.new_from_file("images/IMG_4618.jpg") + im2 = im.shrink(4, 4) + self.assertEqual(im2.width, im.width // 4) + self.assertEqual(im2.height, im.height // 4) + + def test_similarity(self): + im = Vips.Image.new_from_file("images/IMG_4618.jpg") + im2 = im.similarity(angle = 90) + im3 = im.affine([0, -1, 1, 0]) + im2.write_to_file("im2.v") + im3.write_to_file("im3.v") + # not equal ?!?!?! + self.assertEqual((im2 - im3).abs().max(), 0) + + im = Vips.Image.new_from_file("images/IMG_4618.jpg") + # attempt to read unset angle prop + im2 = im.similarity(scale = 2) + im3 = im.affine([2, 0, 0, 2]) + im2.write_to_file("im2.v") + im3.write_to_file("im3.v") + self.assertEqual((im2 - im3).abs().max(), 0) + +if __name__ == '__main__': + unittest.main()