fix up the vips8 python binding
This commit is contained in:
parent
2e4ca3211a
commit
2ba3a1a674
@ -9,17 +9,17 @@ from gi.repository import Vips
|
||||
Vips.cache_set_trace(True)
|
||||
|
||||
try:
|
||||
a = vips.Image("/home/john/pics/babe.poop")
|
||||
except vips.Error, e:
|
||||
a = Vips.Image.new_from_file("/home/john/pics/babe.poop")
|
||||
except Vips.Error, e:
|
||||
print e
|
||||
|
||||
a = vips.Image("/home/john/pics/babe.jpg")
|
||||
b = vips.Image("/home/john/pics/k2.jpg")
|
||||
a = Vips.Image.new_from_file("/home/john/pics/babe.jpg")
|
||||
b = Vips.Image.new_from_file("/home/john/pics/k2.jpg")
|
||||
|
||||
print 'a =', a
|
||||
print 'b =', b
|
||||
|
||||
out = vips.call("add", a, b)
|
||||
out = Vips.call("add", a, b)
|
||||
|
||||
print 'out =', out
|
||||
|
||||
@ -27,4 +27,5 @@ out = a.add(b)
|
||||
|
||||
print 'out =', out
|
||||
|
||||
out = a.linear(1, 2)
|
||||
# we need to get GBoxed working for this
|
||||
#out = a.linear(1, 2)
|
||||
|
@ -1,12 +1,17 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import sys
|
||||
|
||||
#import logging
|
||||
#logging.basicConfig(level = logging.DEBUG)
|
||||
|
||||
from vips8 import vips
|
||||
|
||||
from gi.repository import Vips
|
||||
|
||||
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()
|
||||
|
||||
|
@ -1,11 +1,16 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import sys
|
||||
from vips8 import vips
|
||||
from gi.repository import Vips
|
||||
|
||||
a = vips.Image(sys.argv[1])
|
||||
b = vips.Image(sys.argv[2])
|
||||
#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])
|
||||
|
||||
b = Vips.Image.new_from_file(sys.argv[2])
|
||||
|
||||
c = a.join(b, Vips.Direction.HORIZONTAL, expand = True)
|
||||
|
||||
|
64
python/vips8/vips.py
Executable file → Normal file
64
python/vips8/vips.py
Executable file → Normal file
@ -1,14 +1,15 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import logging
|
||||
import sys
|
||||
|
||||
import logging
|
||||
|
||||
from gi.repository import GLib
|
||||
from gi.repository import GObject
|
||||
|
||||
# you might need this in your .bashrc
|
||||
# export GI_TYPELIB_PATH=$VIPSHOME/lib/girepository-1.0
|
||||
from gi.repository import Vips
|
||||
from gi.repository import Vips
|
||||
|
||||
class Error(Exception):
|
||||
|
||||
@ -28,7 +29,7 @@ class Error(Exception):
|
||||
logging.debug('vips: Error %s %s', self.message, self.detail)
|
||||
|
||||
def __str__(self):
|
||||
return '%s %s' % (self.message, self.detail)
|
||||
return '%s\n %s' % (self.message, self.detail)
|
||||
|
||||
class Argument:
|
||||
def __init__(self, op, prop):
|
||||
@ -76,7 +77,7 @@ def _call_base(name, self, required, optional):
|
||||
|
||||
if len(required_input) != len(required):
|
||||
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)))
|
||||
|
||||
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
|
||||
op2.unref_outputs()
|
||||
|
||||
logging.debug('success, out = %s' % out)
|
||||
|
||||
return out
|
||||
|
||||
# general user entrypoint
|
||||
def call(name, *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):
|
||||
return _call_base(name, self, args, kwargs)
|
||||
|
||||
class Image(Vips.Image):
|
||||
def __init__(self, filename = None, mode = None):
|
||||
Vips.Image.__init__(self)
|
||||
# this is a class method
|
||||
def vips_image_new_from_file(cls, filename, **kwargs):
|
||||
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:
|
||||
self.props.filename = filename
|
||||
if not mode:
|
||||
mode = "rd"
|
||||
if mode:
|
||||
self.props.mode = mode
|
||||
return _call_base(loader, None, [filename], kwargs)
|
||||
|
||||
if self.build() != 0:
|
||||
print 'build failed'
|
||||
raise Error('Unable to build image')
|
||||
def vips_image_getattr(self, name):
|
||||
logging.debug('Image.__getattr__ %s' % name)
|
||||
|
||||
def __getattr__(self, name):
|
||||
logging.debug('vipsimage: __getattr__ %s' % name)
|
||||
return lambda *args, **kwargs: _call_instance(self, name, args, kwargs)
|
||||
# look up in props first, eg. x.props.width
|
||||
if name in dir(self.props):
|
||||
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!
|
||||
Vips.init(sys.argv[0])
|
||||
|
Loading…
Reference in New Issue
Block a user