libvips/python/test_conversion.py

223 lines
8.1 KiB
Python
Raw Normal View History

2014-09-22 14:50:09 +02:00
#!/usr/bin/python
import unittest
import math
#import logging
#logging.basicConfig(level = logging.DEBUG)
from gi.repository import Vips
from vips8 import vips
unsigned_formats = [Vips.BandFormat.UCHAR,
Vips.BandFormat.USHORT,
Vips.BandFormat.UINT]
signed_formats = [Vips.BandFormat.CHAR,
Vips.BandFormat.SHORT,
Vips.BandFormat.INT]
float_formats = [Vips.BandFormat.FLOAT,
Vips.BandFormat.DOUBLE]
complex_formats = [Vips.BandFormat.COMPLEX,
Vips.BandFormat.DPCOMPLEX]
int_formats = unsigned_formats + signed_formats
noncomplex_formats = int_formats + float_formats
all_formats = int_formats + float_formats + complex_formats
# an expanding zip ... if either of the args is not a list, duplicate it down
# the other
def zip_expand(x, y):
if isinstance(x, list) and isinstance(y, list):
return 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 TestConversion(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)
# run a function on an image and on a single pixel, the results
# should match
2014-09-25 10:58:51 +02:00
def run_cmp_unary(self, message, im, x, y, fn):
2014-09-22 14:50:09 +02:00
a = im.getpoint(x, y)
v1 = fn(a)
im2 = fn(im)
v2 = im2.getpoint(x, y)
self.assertAlmostEqualObjects(v1, v2, msg = message)
# run a function on a pair of images and on a pair of pixels, the results
# should match
2014-09-25 10:58:51 +02:00
def run_cmp_binary(self, message, left, right, x, y, fn):
2014-09-22 14:50:09 +02:00
a = left.getpoint(x, y)
b = right.getpoint(x, y)
v1 = fn(a, b)
after = fn(left, right)
v2 = after.getpoint(x, y)
self.assertAlmostEqualObjects(v1, v2, msg = message)
# run a function on a pair of images
# 50,50 and 10,10 should have different values on the test image
2014-09-25 10:58:51 +02:00
def run_testbinary(self, message, left, right, fn):
self.run_cmp_binary(message, left, right, 50, 50, fn)
self.run_cmp_binary(message, left, right, 10, 10, fn)
2014-09-22 14:50:09 +02:00
# run a function on an image,
# 50,50 and 10,10 should have different values on the test image
def run_testunary(self, message, im, fn):
2014-09-25 10:58:51 +02:00
self.run_cmp_unary(message, im, 50, 50, fn)
self.run_cmp_unary(message, im, 10, 10, fn)
2014-09-22 14:50:09 +02:00
def run_unary(self, images, fn, fmt = all_formats):
2014-09-25 10:58:51 +02:00
[self.run_testunary(fn.func_name + (' %s' % y), x.cast(y), fn)
2014-09-22 14:50:09 +02:00
for x in images for y in fmt]
2014-09-25 10:58:51 +02:00
def run_binary(self, images, fn, fmt = all_formats):
[self.run_testbinary(fn.func_name + (' %s %s' % (y, z)),
x.cast(y), x.cast(z), fn)
for x in images for y in fmt for z in fmt]
2014-09-22 14:50:09 +02:00
def setUp(self):
im = Vips.Image.mask_ideal(100, 100, 0.5, reject = True, optical = True)
self.colour = im * [1, 2, 3] + [2, 3, 4]
self.mono = self.colour.extract_band(1)
self.all_images = [self.mono, self.colour]
def test_band_and(self):
def band_and(x):
if isinstance(x, Vips.Image):
return x.bandbool(Vips.OperationBoolean.AND)
else:
return [reduce(lambda a, b: int(a) & int(b), x)]
self.run_unary([self.colour], band_and, fmt = int_formats)
def test_band_or(self):
def band_or(x):
if isinstance(x, Vips.Image):
return x.bandbool(Vips.OperationBoolean.OR)
else:
return [reduce(lambda a, b: int(a) | int(b), x)]
self.run_unary([self.colour], band_or, fmt = int_formats)
def test_band_eor(self):
def band_eor(x):
if isinstance(x, Vips.Image):
return x.bandbool(Vips.OperationBoolean.EOR)
else:
return [reduce(lambda a, b: int(a) ^ int(b), x)]
self.run_unary([self.colour], band_eor, fmt = int_formats)
2014-09-24 22:57:56 +02:00
def test_bandjoin(self):
2014-09-25 10:58:51 +02:00
def bandjoin(x, y):
if isinstance(x, Vips.Image) and isinstance(y, Vips.Image):
return x.bandjoin2(y)
else:
return x + y
self.run_binary(self.all_images, bandjoin)
def test_bandmean(self):
def bandmean(x):
if isinstance(x, Vips.Image):
return x.bandmean()
else:
return [sum(x) / len(x)]
self.run_unary([self.colour], bandmean, fmt = noncomplex_formats)
def test_bandrank(self):
def median(x, y):
joined = zip(x, y)
map(lambda x: list(x).sort(), joined)
return map(lambda x: x[len(x) / 2], joined)
def bandrank(x, y):
if isinstance(x, Vips.Image) and isinstance(y, Vips.Image):
return Vips.Image.bandrank([x, y])
else:
return median(x, y)
2014-09-24 22:57:56 +02:00
2014-09-25 10:58:51 +02:00
self.run_binary(self.all_images, bandrank, fmt = noncomplex_formats)
2014-09-24 22:57:56 +02:00
2014-09-25 15:00:17 +02:00
def test_cache(self):
def cache(x):
if isinstance(x, Vips.Image):
return x.cache()
else:
return x
self.run_unary(self.all_images, cache)
def test_copy(self):
x = self.colour.copy(interpretation = Vips.Interpretation.LAB)
self.assertEqual(x.interpretation, Vips.Interpretation.LAB)
x = self.colour.copy(xres = 42)
self.assertEqual(x.xres, 42)
x = self.colour.copy(yres = 42)
self.assertEqual(x.yres, 42)
x = self.colour.copy(xoffset = 42)
self.assertEqual(x.xoffset, 42)
x = self.colour.copy(yoffset = 42)
self.assertEqual(x.yoffset, 42)
x = self.colour.copy(bands = 1)
self.assertEqual(x.bands, 1)
x = self.colour.copy(format = Vips.BandFormat.USHORT, bands = 1)
self.assertEqual(x.format, Vips.BandFormat.USHORT)
x = self.colour.copy(coding = Vips.Coding.NONE)
self.assertEqual(x.coding, Vips.Coding.NONE)
x = self.colour.copy(width = 42)
self.assertEqual(x.width, 42)
x = self.colour.copy(height = 42)
self.assertEqual(x.height, 42)
def test_embed(self):
im = self.colour.embed(20, 20,
self.colour.width + 40,
self.colour.height + 40)
pixel = im.getpoint(10, 10)
self.assertAlmostEqualObjects(pixel, [0, 0, 0])
pixel = im.getpoint(30, 30)
self.assertAlmostEqualObjects(pixel, [2, 3, 4])
pixel = im.getpoint(im.width - 10, im.height - 10)
self.assertAlmostEqualObjects(pixel, [0, 0, 0])
im = self.colour.embed(20, 20,
self.colour.width + 40,
self.colour.height + 40,
extend = Vips.Extend.COPY)
pixel = im.getpoint(10, 10)
self.assertAlmostEqualObjects(pixel, [2, 3, 4])
pixel = im.getpoint(im.width - 10, im.height - 10)
self.assertAlmostEqualObjects(pixel, [2, 3, 4])
im = self.colour.embed(20, 20,
self.colour.width + 40,
self.colour.height + 40,
extend = Vips.Extend.BACKGROUND,
background = [7, 8, 9])
pixel = im.getpoint(10, 10)
self.assertAlmostEqualObjects(pixel, [7, 8, 9])
pixel = im.getpoint(im.width - 10, im.height - 10)
self.assertAlmostEqualObjects(pixel, [7, 8, 9])
im = self.colour.embed(20, 20,
self.colour.width + 40,
self.colour.height + 40,
extend = Vips.Extend.WHITE)
pixel = im.getpoint(10, 10)
self.assertAlmostEqualObjects(pixel, [255, 255, 255])
pixel = im.getpoint(im.width - 10, im.height - 10)
self.assertAlmostEqualObjects(pixel, [255, 255, 255])
2014-09-22 14:50:09 +02:00
if __name__ == '__main__':
unittest.main()