fix up ifthenelse and set_value

This commit is contained in:
John Cupitt 2014-10-16 10:58:54 +01:00
parent fc4c8b9cd7
commit a8f4ed7871
3 changed files with 29 additions and 13 deletions

11
TODO
View File

@ -1,17 +1,8 @@
- python:
- constants for images:
- update examples with new image constant rules
- support a.set_value('x', 2), where 'x' is an image arg, or
an imagearray arg
- c.ifthenelse(t, e) ... if t or e are constants, we need to
match to the other branch of the conditional, not to the
constant
maybe match to the constant if both t and e are numbers
- do more tests
- put exif autorotate into jpeg load

View File

@ -590,14 +590,19 @@ class Image(Vips.Image):
logging.debug('assigning %s to %s' % (value, self))
logging.debug('%s needs a %s' % (self, gtype))
# array-ize some types, if necessary
value = arrayize(gtype, value)
# blob-ize
if GObject.type_is_a(gtype, vips_type_blob):
if not isinstance(value, Vips.Blob):
value = Vips.Blob.new(None, value)
# image-ize
if GObject.type_is_a(gtype, vips_type_image):
if not isinstance(value, Vips.Image):
value = imageize(match_image, value)
# array-ize some types, if necessary
value = arrayize(gtype, value)
self.set(field, value)
def floor(self):
@ -675,6 +680,20 @@ class Image(Vips.Image):
def exp10(self):
return self.math(Vips.OperationMath.EXP10)
# we need different imageize rules for this operator ... we need to
# imageize th and el to match each other first
def ifthenelse(self, th, el, **kwargs):
for match_image in [th, el, self]:
if isinstance(match_image, Vips.Image):
break
if not isinstance(th, Vips.Image):
th = imageize(match_image, th)
if not isinstance(el, Vips.Image):
el = imageize(match_image, el)
return _call_base("ifthenelse", [th, el], kwargs, self)
# add operators which needs to be class methods
# use find_class_methods.py to generate this list

View File

@ -11,3 +11,9 @@ a = Vips.Image.black(100, 100)
b = a.bandjoin(2)
b.write_to_file("x.v")
txt = Vips.Image.text("left corner", dpi = 300)
c = txt.ifthenelse(2, [0, 255, 0], blend = True)
c.write_to_file("x2.v")