fix up the vips8 python binding

This commit is contained in:
John Cupitt 2014-08-29 17:33:14 +01:00
parent 2e4ca3211a
commit 2ba3a1a674
4 changed files with 66 additions and 31 deletions

View File

@ -9,17 +9,17 @@ from gi.repository import Vips
Vips.cache_set_trace(True) Vips.cache_set_trace(True)
try: try:
a = vips.Image("/home/john/pics/babe.poop") a = Vips.Image.new_from_file("/home/john/pics/babe.poop")
except vips.Error, e: except Vips.Error, e:
print e print e
a = vips.Image("/home/john/pics/babe.jpg") a = Vips.Image.new_from_file("/home/john/pics/babe.jpg")
b = vips.Image("/home/john/pics/k2.jpg") b = Vips.Image.new_from_file("/home/john/pics/k2.jpg")
print 'a =', a print 'a =', a
print 'b =', b print 'b =', b
out = vips.call("add", a, b) out = Vips.call("add", a, b)
print 'out =', out print 'out =', out
@ -27,4 +27,5 @@ out = a.add(b)
print 'out =', out print 'out =', out
out = a.linear(1, 2) # we need to get GBoxed working for this
#out = a.linear(1, 2)

View File

@ -1,12 +1,17 @@
#!/usr/bin/python #!/usr/bin/python
import sys import sys
#import logging
#logging.basicConfig(level = logging.DEBUG)
from vips8 import vips from vips8 import vips
from gi.repository import Vips from gi.repository import Vips
Vips.cache_set_trace(True) Vips.cache_set_trace(True)
a = vips.Image(sys.argv[1]) a = Vips.Image.new_from_file(sys.argv[1])
print a.max() print a.max()
print a.max() print a.max()

View File

@ -1,11 +1,16 @@
#!/usr/bin/python #!/usr/bin/python
import sys import sys
from vips8 import vips
from gi.repository import Vips
a = vips.Image(sys.argv[1]) #import logging
b = vips.Image(sys.argv[2]) #logging.basicConfig(level = logging.DEBUG)
from gi.repository import Vips
from vips8 import vips
a = Vips.Image.new_from_file(sys.argv[1])
b = Vips.Image.new_from_file(sys.argv[2])
c = a.join(b, Vips.Direction.HORIZONTAL, expand = True) c = a.join(b, Vips.Direction.HORIZONTAL, expand = True)

64
python/vips8/vips.py Executable file → Normal file
View File

@ -1,14 +1,15 @@
#!/usr/bin/python #!/usr/bin/python
import logging
import sys import sys
import logging
from gi.repository import GLib from gi.repository import GLib
from gi.repository import GObject from gi.repository import GObject
# you might need this in your .bashrc # you might need this in your .bashrc
# export GI_TYPELIB_PATH=$VIPSHOME/lib/girepository-1.0 # export GI_TYPELIB_PATH=$VIPSHOME/lib/girepository-1.0
from gi.repository import Vips from gi.repository import Vips
class Error(Exception): class Error(Exception):
@ -28,7 +29,7 @@ class Error(Exception):
logging.debug('vips: Error %s %s', self.message, self.detail) logging.debug('vips: Error %s %s', self.message, self.detail)
def __str__(self): def __str__(self):
return '%s %s' % (self.message, self.detail) return '%s\n %s' % (self.message, self.detail)
class Argument: class Argument:
def __init__(self, op, prop): def __init__(self, op, prop):
@ -76,7 +77,7 @@ def _call_base(name, self, required, optional):
if len(required_input) != len(required): if len(required_input) != len(required):
raise Error('Wrong number of arguments.', raise Error('Wrong number of arguments.',
'"%s" needs %d arguments, you supplied %d' % '%s needs %d arguments, you supplied %d' %
(name, len(required_input), len(required))) (name, len(required_input), len(required)))
for i in range(len(required_input)): for i in range(len(required_input)):
@ -130,34 +131,57 @@ def _call_base(name, self, required, optional):
# unref everything now we have refs to all outputs we want # unref everything now we have refs to all outputs we want
op2.unref_outputs() op2.unref_outputs()
logging.debug('success, out = %s' % out)
return out return out
# general user entrypoint # general user entrypoint
def call(name, *args, **kwargs): def call(name, *args, **kwargs):
return _call_base(name, None, args, kwargs) return _call_base(name, None, args, kwargs)
# from getattr ... try to run the attr as a method # here from getattr ... try to run the attr as a method
def _call_instance(self, name, args, kwargs): def _call_instance(self, name, args, kwargs):
return _call_base(name, self, args, kwargs) return _call_base(name, self, args, kwargs)
class Image(Vips.Image): # this is a class method
def __init__(self, filename = None, mode = None): def vips_image_new_from_file(cls, filename, **kwargs):
Vips.Image.__init__(self) loader = Vips.Foreign.find_load(filename)
if loader == None:
raise Error('No known loader for "%s".' % filename)
logging.debug('Image.new_from_file: loader = %s' % loader)
if filename: return _call_base(loader, None, [filename], kwargs)
self.props.filename = filename
if not mode:
mode = "rd"
if mode:
self.props.mode = mode
if self.build() != 0: def vips_image_getattr(self, name):
print 'build failed' logging.debug('Image.__getattr__ %s' % name)
raise Error('Unable to build image')
def __getattr__(self, name): # look up in props first, eg. x.props.width
logging.debug('vipsimage: __getattr__ %s' % name) if name in dir(self.props):
return lambda *args, **kwargs: _call_instance(self, name, args, kwargs) return getattr(self.props, name)
return lambda *args, **kwargs: _call_instance(self, name, args, kwargs)
def vips_image_write_to_file(self, filename, **kwargs):
saver = Vips.Foreign.find_save(filename)
if saver == None:
raise Error('No known saver for "%s".' % filename)
logging.debug('Image.write_to_file: saver = %s' % saver)
_call_base(saver, self, [filename], kwargs)
# paste our methods into Vips.Image
# class methods
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
# Add other classes to Vips
Vips.Error = Error
Vips.Argument = Argument
Vips.call = call
# start up vips! # start up vips!
Vips.init(sys.argv[0]) Vips.init(sys.argv[0])