From f03dc3754fed6b74dfb02720dcf30e6bd9f57e48 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Mon, 1 Sep 2014 11:32:17 +0100 Subject: [PATCH] start adding python operator overloads --- TODO | 12 ++++++++++++ python/try4.py | 2 -- python/try5.py | 18 ++++++++++++++++++ python/vips8/vips.py | 24 +++++++++++++++++++++++- 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100755 python/try5.py diff --git a/TODO b/TODO index 21c50c95..a8705c61 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,15 @@ +- mysterious + +from gi.repository import Vips +from vips8 import vips + +a = Vips.Image.new_from_file("/home/john/pics/k2.jpg") +a.linear(1,2) + works +a.linear([1],[2]) + fails !?!? + + - python: - add more constructors diff --git a/python/try4.py b/python/try4.py index 50c4fb29..489f8fb4 100755 --- a/python/try4.py +++ b/python/try4.py @@ -14,6 +14,4 @@ b = Vips.Image.new_from_file(sys.argv[2]) c = a.join(b, Vips.Direction.HORIZONTAL, expand = True) -d = c.linear(2, 3) - d.write_to_file(sys.argv[3]) diff --git a/python/try5.py b/python/try5.py new file mode 100755 index 00000000..e6df5de3 --- /dev/null +++ b/python/try5.py @@ -0,0 +1,18 @@ +#!/usr/bin/python + +import sys + +#import logging +#logging.basicConfig(level = logging.DEBUG) + +from gi.repository import Vips +from vips8 import vips + +a = Vips.Image.new_from_file(sys.argv[1]) + +# test operator overloads +b = a + 12 +b = a + [12, 0, 0] +b = a + b + +b.write_to_file(sys.argv[2]) diff --git a/python/vips8/vips.py b/python/vips8/vips.py index bde30dbf..b72bacb7 100644 --- a/python/vips8/vips.py +++ b/python/vips8/vips.py @@ -14,10 +14,11 @@ from gi.repository import Vips # start up vips! Vips.init(sys.argv[0]) -# need the gtypes for the vips array types +# need the gtypes for various vips types vips_type_array_int = GObject.GType.from_name("VipsArrayInt") vips_type_array_double = GObject.GType.from_name("VipsArrayDouble") vips_type_array_image = GObject.GType.from_name("VipsArrayImage") +vips_type_image = GObject.GType.from_name("VipsImage") class Error(Exception): @@ -203,6 +204,24 @@ def vips_image_write_to_file(self, vips_filename, **kwargs): _call_base(saver, [filename], kwargs, self, option_string) +def vips_add(self, other): + if isinstance(other, Vips.Image): + return self.add(other) + else: + return self.linear(1, other) + +def vips_sub(self, other): + if isinstance(other, Vips.Image): + return self.subtract(other) + else: + return self.linear(1, -1 * other) + +def vips_mul(self, other): + if isinstance(other, Vips.Image): + return self.multiply(other) + else: + return self.linear(other, 0) + # paste our methods into Vips.Image # class methods @@ -211,6 +230,9 @@ setattr(Vips.Image, 'new_from_file', classmethod(vips_image_new_from_file)) # instance methods Vips.Image.write_to_file = vips_image_write_to_file Vips.Image.__getattr__ = vips_image_getattr +Vips.Image.__add__ = vips_add +Vips.Image.__sub__ = vips_sub +Vips.Image.__mul__ = vips_mul # Add other classes to Vips Vips.Error = Error