use new pyvips8 array constant syntax

also add median filter convenience function
This commit is contained in:
John Cupitt 2015-01-29 16:41:11 +00:00
parent 21b4748fe1
commit ddb43172f3
5 changed files with 37 additions and 30 deletions

13
TODO
View File

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

View File

@ -218,9 +218,24 @@ profile = im.get_value("icc-profile-data")
</para>
<para>
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, <code>.ifthenelse()</code> 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
<code>.erode()</code>, for example:
<programlisting language="Python">
im = im.erode([[128, 255, 128],
[255, 255, 255],
[128, 255, 128]])
</programlisting>
will erode an image with a 4-connected structuring element.
</para>
<para>
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, <code>.ifthenelse()</code> uses
a condition image to pick pixels between a then and an else image:
<programlisting language="Python">
@ -241,7 +256,7 @@ result_image = condition_image.ifthenelse([0, 255, 0], [255, 0, 0])
</para>
<para>
This is useful for <code>.bandjoin()</code>, the thing to join
This is also useful for <code>.bandjoin()</code>, the thing to join
two or more images up bandwise. You can write:
<programlisting language="Python">
@ -360,9 +375,12 @@ result_image = image.sin()
<title>Convenience functions</title>
<para>
The wrapper defines a few extra useful utility functions:
<code>.get_value()</code>, <code>.set_value()</code>
<code>.bandsplit()</code>, <code>.maxpos()</code>
<code>.minpos()</code>.
<code>.get_value()</code>,
<code>.set_value()</code>,
<code>.bandsplit()</code>,
<code>.maxpos()</code>,
<code>.minpos()</code>,
<code>.median()</code>.
Again, see <code>help(Vips.Image)</code> for a list.
</para>
</refsect1>

View File

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

View File

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

View File

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