use bytes() for VipsBlob in python3

This commit is contained in:
John Cupitt 2014-12-11 13:08:14 +00:00
parent d5c164898e
commit b4b8b8bd96
2 changed files with 19 additions and 25 deletions

2
TODO
View File

@ -1,5 +1,3 @@
- do binary / text string split in Vips.py
- use vips_resize() in vipsthumbnail? - use vips_resize() in vipsthumbnail?
should the sharpening filter be selectable? should the sharpening filter be selectable?

View File

@ -67,12 +67,20 @@ def imageize(match_image, value):
extend = Vips.Extend.COPY) extend = Vips.Extend.COPY)
return image return image
unpack_types = [Vips.Blob, Vips.ArrayDouble, Vips.ArrayImage, Vips.ArrayInt] # we'd like to use memoryview to avoid copying things like ICC profiles, but
def isunpack(obj): # unfortunately pygobject does not support this ... so for blobs we just use
for t in unpack_types: # bytes().
if isinstance(obj, t):
return True unpack_types = [[Vips.Blob, bytes],
return False [Vips.ArrayDouble, lambda x: x.get()],
[Vips.ArrayImage, lambda x: x.get()],
[Vips.ArrayInt, lambda x: x.get()]]
def unpack(value):
for t, cast in unpack_types:
if isinstance(value, t):
return cast(value)
return value
def array_image_new(array): def array_image_new(array):
match_image = None match_image = None
@ -99,7 +107,7 @@ def arrayize(gtype, value):
if GObject.type_is_a(gtype, t): if GObject.type_is_a(gtype, t):
if not isinstance(value, list): if not isinstance(value, list):
value = [value] value = [value]
value = cast(value) return cast(value)
return value return value
@ -167,13 +175,7 @@ class Argument(object):
logging.debug('read out %s from %s' % (value, self.name)) logging.debug('read out %s from %s' % (value, self.name))
# turn VipsBlobs into strings, VipsArrayDouble into lists etc. return unpack(value)
# FIXME ... this will involve a copy, we should use
# buffer() instead
if isunpack(value):
value = value.get()
return value
def description(self): def description(self):
result = self.name result = self.name
@ -742,25 +744,19 @@ class Image(Vips.Image):
"""Get a named item from an Image. """Get a named item from an Image.
Fetch an item of metadata and convert it to a Python-friendly format. Fetch an item of metadata and convert it to a Python-friendly format.
For example, VipsBlob things will be converted to strings. For example, VipsBlob values will be converted to bytes().
""" """
value = self.get(field) value = self.get(field)
logging.debug('read out %s from %s' % (value, self)) logging.debug('read out %s from %s' % (value, self))
# turn VipsBlobs into strings, VipsArrayDouble into lists etc. return unpack(value)
# FIXME ... this will involve a copy, we should use
# buffer() instead
if isunpack(value):
value = value.get()
return value
def set_value(self, field, value): def set_value(self, field, value):
"""Set a named item on an Image. """Set a named item on an Image.
Values are converted from Python types to something libvips can swallow. Values are converted from Python types to something libvips can swallow.
For example, strings can be used to set VipsBlob fields. For example, bytes() can be used to set VipsBlob fields.
""" """
gtype = self.get_typeof(field) gtype = self.get_typeof(field)
logging.debug('assigning %s to %s' % (value, self)) logging.debug('assigning %s to %s' % (value, self))