fix optional output args in python

oh arg we have to return a dict

also, test hough circle and line, and add maxpos/minpos
This commit is contained in:
John Cupitt 2014-09-16 10:47:24 +01:00
parent 1c1f7a205a
commit 4968b9edaf
3 changed files with 43 additions and 11 deletions

6
TODO
View File

@ -1,12 +1,6 @@
- python: - python:
- maxpos?
perhaps:
v, x, y = im.max(x = True, y = True)
- could import like this: - could import like this:
from gi.repository import Vips from gi.repository import Vips

View File

@ -1,7 +1,6 @@
#!/usr/bin/python #!/usr/bin/python
import unittest import unittest
import gc
#import logging #import logging
#logging.basicConfig(level = logging.DEBUG) #logging.basicConfig(level = logging.DEBUG)
@ -359,12 +358,34 @@ class TestArithmetic(unittest.TestCase):
self.assertEqual(hist.bands, 1) self.assertEqual(hist.bands, 1)
def test_hough_circle(self): def test_hough_circle(self):
im = Vips.Image.black(100, 100).draw_circle(100, 50, 50, 40) test = Vips.Image.black(100, 100).draw_circle(100, 50, 50, 40)
hough = im.hough_circle(min_radius = 35, max_radius = 45) for fmt in all_formats:
im = test.cast(fmt)
hough = im.hough_circle(min_radius = 35, max_radius = 45)
v, x, y = hough.maxpos()
vec = hough.getpoint(x, y)
r = vec.index(v) + 35
self.assertAlmostEqual(x, 50)
self.assertAlmostEqual(y, 50)
self.assertAlmostEqual(r, 40)
def test_hough_line(self):
test = Vips.Image.black(100, 100).draw_line(100, 10, 90, 90, 10)
for fmt in all_formats:
im = test.cast(fmt)
hough = im.hough_line()
v, x, y = hough.maxpos()
angle = 360.0 * x / hough.width
distance = test.height * y / hough.height
self.assertAlmostEqual(angle, 45)
self.assertAlmostEqual(distance, 70)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -199,9 +199,12 @@ def _call_base(name, required, optional, self = None, option_string = None):
if x.flags & enm.INPUT and x.flags & enm.MODIFY: if x.flags & enm.INPUT and x.flags & enm.MODIFY:
out.append(x.get_value()) out.append(x.get_value())
out_dict = {}
for x in optional.keys(): for x in optional.keys():
if x in optional_output: if x in optional_output:
out.append(optional_output[x].get_value()) out_dict[x] = optional_output[x].get_value()
if out_dict != {}:
out.append(out_dict)
if len(out) == 1: if len(out) == 1:
out = out[0] out = out[0]
@ -304,6 +307,18 @@ def vips_image_write_to_buffer(self, vips_filename, **kwargs):
def vips_bandsplit(self): def vips_bandsplit(self):
return [self.extract_band(i) for i in range(0, self.bands)] return [self.extract_band(i) for i in range(0, self.bands)]
def vips_maxpos(self):
v, opts = self.max(x = True, y = True)
x = opts['x']
y = opts['y']
return v, x, y
def vips_minpos(self):
v, opts = self.min(x = True, y = True)
x = opts['x']
y = opts['y']
return v, x, y
# apply a function to a thing, or map over a list # apply a function to a thing, or map over a list
# we often need to do something like (1.0 / other) and need to work for lists # we often need to do something like (1.0 / other) and need to work for lists
# as well as scalars # as well as scalars
@ -480,6 +495,8 @@ Vips.Image.write_to_buffer = vips_image_write_to_buffer
# a few useful things # a few useful things
Vips.Image.floor = vips_floor Vips.Image.floor = vips_floor
Vips.Image.bandsplit = vips_bandsplit Vips.Image.bandsplit = vips_bandsplit
Vips.Image.maxpos = vips_maxpos
Vips.Image.minpos = vips_minpos
Vips.Image.real = vips_real Vips.Image.real = vips_real
Vips.Image.imag = vips_imag Vips.Image.imag = vips_imag
Vips.Image.polar = vips_polar Vips.Image.polar = vips_polar