started test_resample.py

This commit is contained in:
John Cupitt 2015-01-29 09:29:10 +00:00
parent c384e2b664
commit 2a1a371e5c
6 changed files with 90 additions and 2 deletions

2
TODO
View File

@ -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],

View File

@ -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

View File

@ -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

View File

@ -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()

View File

@ -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

79
test/test_resample.py Executable file
View File

@ -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()