diff --git a/TODO b/TODO index 26c9171b..7d5c2e51 100644 --- a/TODO +++ b/TODO @@ -1,17 +1,4 @@ -- add imageize from 2D arrays, eg. we should be able to write - - im = im.erode([[128, 255, 128], - [255, 255, 255], - [128, 255, 128]]) - - see test_morphology.py - - seems to work ... add note to docs, update morph and conv tests - -- add median convenience function, see test_morphology.py - - - use vips_resize() in vipsthumbnail? diff --git a/doc/reference/using-python.xml b/doc/reference/using-python.xml index 123ea99f..93d985c9 100644 --- a/doc/reference/using-python.xml +++ b/doc/reference/using-python.xml @@ -218,9 +218,24 @@ profile = im.get_value("icc-profile-data") - If an operation takes several input images, you can use a constant - for all but one of them and the wrapper will expand the constant - to an image for you. For example, .ifthenelse() uses + You can use array constants instead of images. A 2D array is simply + changed into a one-band double image. This is handy for things like + .erode(), for example: + + +im = im.erode([[128, 255, 128], + [255, 255, 255], + [128, 255, 128]]) + + + will erode an image with a 4-connected structuring element. + + + + If an operation takes several input images, you can use a 1D array + constant or a number constant + for all but one of them and the wrapper will expand it + to an image for you. For example, .ifthenelse() uses a condition image to pick pixels between a then and an else image: @@ -241,7 +256,7 @@ result_image = condition_image.ifthenelse([0, 255, 0], [255, 0, 0]) - This is useful for .bandjoin(), the thing to join + This is also useful for .bandjoin(), the thing to join two or more images up bandwise. You can write: @@ -360,9 +375,12 @@ result_image = image.sin() Convenience functions The wrapper defines a few extra useful utility functions: - .get_value(), .set_value() - .bandsplit(), .maxpos() - .minpos(). + .get_value(), + .set_value(), + .bandsplit(), + .maxpos(), + .minpos(), + .median(). Again, see help(Vips.Image) for a list. diff --git a/python/Vips.py b/python/Vips.py index 7ffd379f..4ce5b329 100644 --- a/python/Vips.py +++ b/python/Vips.py @@ -901,6 +901,10 @@ class Image(Vips.Image): """Dilate with a structuring element.""" return self.morph(mask, Vips.OperationMorphology.DILATE) + def median(self, size): + """size x size median filter.""" + return self.rank(size, size, (size * size) / 2) + # we need different imageize rules for this operator ... we need to # imageize th and el to match each other first @add_doc(generate_docstring("ifthenelse")) diff --git a/test/test_conversion.py b/test/test_conversion.py index 70d2461b..5710a637 100755 --- a/test/test_conversion.py +++ b/test/test_conversion.py @@ -566,12 +566,11 @@ class TestConversion(unittest.TestCase): self.assertAlmostEqualObjects(result, predict) def test_recomb(self): - array = [[0.2, 0.5, 0.3]] - mask = Vips.Image.new_from_array(array) + array = [[0.2, 0.5, 0.3]] def recomb(x): if isinstance(x, Vips.Image): - return x.recomb(mask) + return x.recomb(array) else: sum = 0 for i, c in zip(array[0], x): diff --git a/test/test_morphology.py b/test/test_morphology.py index 44b86cf7..a7862004 100755 --- a/test/test_morphology.py +++ b/test/test_morphology.py @@ -33,11 +33,6 @@ class TestMorphology(unittest.TestCase): for x, y in zip_expand(a, b): self.assertAlmostEqual(x, y, places = places, msg = msg) - def setUp(self): - self.mask = Vips.Image.new_from_array([[128, 255, 128], - [255, 255, 255], - [128, 255, 128]]) - def test_countlines(self): im = Vips.Image.black(100, 100) im = im.draw_line(255, 0, 50, 100, 50) @@ -55,7 +50,9 @@ class TestMorphology(unittest.TestCase): def test_erode(self): im = Vips.Image.black(100, 100) im = im.draw_circle(255, 50, 50, 25, fill = True) - im2 = im.erode(self.mask) + im2 = im.erode([[128, 255, 128], + [255, 255, 255], + [128, 255, 128]]) self.assertEqual(im.width, im2.width) self.assertEqual(im.height, im2.height) self.assertEqual(im.bands, im2.bands) @@ -64,7 +61,9 @@ class TestMorphology(unittest.TestCase): def test_dilate(self): im = Vips.Image.black(100, 100) im = im.draw_circle(255, 50, 50, 25, fill = True) - im2 = im.dilate(self.mask) + im2 = im.dilate([[128, 255, 128], + [255, 255, 255], + [128, 255, 128]]) self.assertEqual(im.width, im2.width) self.assertEqual(im.height, im2.height) self.assertEqual(im.bands, im2.bands)