def new_from_file(vips_filename, **kwargs): """Load an image from a file. This method can load images in any format supported by vips. The filename can include load options, for example:: image = pyvips.Image.new_from_file('fred.jpg[shrink=2]') You can also supply options as keyword arguments, for example:: image = pyvips.Image.new_from_file('fred.jpg', shrink=2) The full set of options available depend upon the load operation that will be executed. Try something like:: $ vips jpegload at the command-line to see a summary of the available options for the JPEG loader. Loading is fast: only enough of the image is loaded to be able to fill out the header. Pixels will only be decompressed when they are needed. Args: vips_filename (str): The disc file to load the image from, with optional appended arguments. All loaders support at least the following options: Keyword args: memory (bool): If set True, load the image via memory rather than via a temporary disc file. See :meth:`.new_temp_file` for notes on where temporary files are created. Small images are loaded via memory by default, use ``VIPS_DISC_THRESHOLD`` to set the definition of small. access (Access): Hint the expected access pattern for the image. fail (bool): If set True, the loader will fail with an error on the first serious error in the file. By default, libvips will attempt to read everything it can from a damanged image. Returns: A new :class:`.Image`. Raises: :class:`.Error` """ vips_filename = _to_bytes(vips_filename) filename = vips_lib.vips_filename_get_filename(vips_filename) options = vips_lib.vips_filename_get_options(vips_filename) name = vips_lib.vips_foreign_find_load(filename) if name == ffi.NULL: raise Error('unable to load from file {0}'.format(vips_filename)) return pyvips.Operation.call(_to_string(ffi.string(name)), _to_string(ffi.string(filename)), string_options=_to_string( ffi.string(options) ), **kwargs)
def write_to_file(self, vips_filename, **kwargs): """Write an image to a file on disc. This method can save images in any format supported by vips. The format is selected from the filename suffix. The filename can include embedded save options, see :func:`Image.new_from_file`. For example:: image.write_to_file('fred.jpg[Q=95]') You can also supply options as keyword arguments, for example:: image.write_to_file('fred.jpg', Q=95) The full set of options available depend upon the load operation that will be executed. Try something like:: $ vips jpegsave at the command-line to see a summary of the available options for the JPEG saver. Args: vips_filename (str): The disc file to save the image to, with optional appended arguments. Other arguments depend upon the save operation. Returns: None Raises: :class:`.Error` """ vips_filename = _to_bytes(vips_filename) filename = vips_lib.vips_filename_get_filename(vips_filename) options = vips_lib.vips_filename_get_options(vips_filename) name = vips_lib.vips_foreign_find_save(filename) if name == ffi.NULL: raise Error('unable to write to file {0}'.format(vips_filename)) return pyvips.Operation.call(_to_string(ffi.string(name)), self, filename, string_options=_to_string( ffi.string(options)), **kwargs)
def new_from_buffer(data, options, **kwargs): """Load a formatted image from memory. This behaves exactly as :meth:`new_from_file`, but the image is loaded from the memory object rather than from a file. The memory object can be a string or buffer. Args: data (str, buffer): The memory object to load the image from. options (str): Load options as a string. Use ``""`` for no options. All loaders support at least the following options: Keyword args: access (Access): Hint the expected access pattern for the image. fail (bool): If set True, the loader will fail with an error on the first serious error in the image. By default, libvips will attempt to read everything it can from a damanged image. Returns: A new :class:`Image`. Raises: :class:`.Error` """ name = vips_lib.vips_foreign_find_load_buffer(data, len(data)) if name == ffi.NULL: raise Error('unable to load from buffer') return pyvips.Operation.call(_to_string(ffi.string(name)), data, string_options=options, **kwargs)
def write_to_buffer(self, format_string, **kwargs): """Write an image to memory. This method can save images in any format supported by vips. The format is selected from the suffix in the format string. This can include embedded save options, see :func:`Image.new_from_file`. For example:: data = image.write_to_buffer('.jpg[Q=95]') You can also supply options as keyword arguments, for example:: data = image.write_to_buffer('.jpg', Q=95) The full set of options available depend upon the load operation that will be executed. Try something like:: $ vips jpegsave_buffer at the command-line to see a summary of the available options for the JPEG saver. Args: format_string (str): The suffix, plus any string-form arguments. Other arguments depend upon the save operation. Returns: A byte string. Raises: :class:`.Error` """ format_string = _to_bytes(format_string) options = vips_lib.vips_filename_get_options(format_string) name = vips_lib.vips_foreign_find_save_buffer(format_string) if name == ffi.NULL: raise Error('unable to write to buffer') return pyvips.Operation.call(_to_string(ffi.string(name)), self, string_options=_to_string( ffi.string(options)), **kwargs)
def __init__(self, message, detail=None): self.message = message if detail is None or detail == "": detail = _to_string(ffi.string(vips_lib.vips_error_buffer())) vips_lib.vips_error_clear() self.detail = detail logger.debug('Error %s %s', self.message, self.detail)
def from_enum(gtype, enum_value): """Turn an int back into an enum string. """ cstr = vips_lib.vips_enum_nick(gtype, enum_value) if cstr == 0: raise Error('value not in enum') return _to_string(ffi.string(cstr))
def _to_string(x): """Convert to a unicode string. If x is a byte string, assume it is utf-8 and decode to a Python unicode string. You must call this on text strings you get back from libvips. """ x = ffi.string(x) if isinstance(x, byte_type): x = x.decode('utf-8') return x
def add_construct(self, pspec, argument_class, argument_instance, a, b): flags = argument_class.flags if (flags & _CONSTRUCT) != 0: name = _to_string(ffi.string(pspec.name)) # libvips uses '-' to separate parts of arg names, but we # need '_' for Python name = name.replace('-', '_') args.append([name, flags]) return ffi.NULL
def values_for_enum(gtype): """Get all values for a enum (gtype).""" g_type_class = gobject_lib.g_type_class_ref(gtype) g_enum_class = ffi.cast('GEnumClass *', g_type_class) values = [] # -1 since we always have a "last" member. for i in range(0, g_enum_class.n_values - 1): value = _to_string(ffi.string(g_enum_class.values[i].value_nick)) values.append(value) return values
def get_fields(self): """Get a list of all the metadata fields on an image. Returns: [string] """ array = vips_lib.vips_image_get_fields(self.pointer) names = [] i = 0 while array[i] != ffi.NULL: name = _to_string(ffi.string(array[i])) names.append(name) glib_lib.g_free(array[i]) i += 1 glib_lib.g_free(array) return names
def nickname_find(gtype): """Return the nickname for a GType.""" return _to_string(ffi.string(vips_lib.vips_nickname_find(gtype)))
def type_name(gtype): """Return the name for a GType.""" return _to_string(ffi.string(gobject_lib.g_type_name(gtype)))
def path_mode7(filename): return _to_string(ffi.string(vips_lib.vips_path_mode7( _to_bytes(filename))))
def get(self): """Get the contents of a GValue. The contents of the GValue are read out as a Python type. """ # logger.debug('GValue.get: self = %s', self) gtype = self.gvalue.g_type fundamental = gobject_lib.g_type_fundamental(gtype) result = None if gtype == GValue.gbool_type: result = bool(gobject_lib.g_value_get_boolean(self.gvalue)) elif gtype == GValue.gint_type: result = gobject_lib.g_value_get_int(self.gvalue) elif gtype == GValue.gdouble_type: result = gobject_lib.g_value_get_double(self.gvalue) elif fundamental == GValue.genum_type: return GValue.from_enum(gtype, gobject_lib.g_value_get_enum(self.gvalue)) elif fundamental == GValue.gflags_type: result = gobject_lib.g_value_get_flags(self.gvalue) elif gtype == GValue.gstr_type: cstr = gobject_lib.g_value_get_string(self.gvalue) if cstr != ffi.NULL: result = _to_string(ffi.string(cstr)) elif gtype == GValue.refstr_type: psize = ffi.new('size_t *') cstr = vips_lib.vips_value_get_ref_string(self.gvalue, psize) result = _to_string(ffi.string(cstr, psize[0])) elif gtype == GValue.image_type: # g_value_get_object() will not add a ref ... that is # held by the gvalue go = gobject_lib.g_value_get_object(self.gvalue) vi = ffi.cast('VipsImage *', go) # we want a ref that will last with the life of the vimage: # this ref is matched by the unref that's attached to finalize # by Image() gobject_lib.g_object_ref(go) result = pyvips.Image(vi) elif gtype == GValue.array_int_type: pint = ffi.new('int *') array = vips_lib.vips_value_get_array_int(self.gvalue, pint) result = [] for i in range(0, pint[0]): result.append(array[i]) elif gtype == GValue.array_double_type: pint = ffi.new('int *') array = vips_lib.vips_value_get_array_double(self.gvalue, pint) result = [] for i in range(0, pint[0]): result.append(array[i]) elif gtype == GValue.array_image_type: pint = ffi.new('int *') array = vips_lib.vips_value_get_array_image(self.gvalue, pint) result = [] for i in range(0, pint[0]): vi = array[i] gobject_lib.g_object_ref(vi) image = pyvips.Image(vi) result.append(image) elif gtype == GValue.blob_type: psize = ffi.new('size_t *') array = vips_lib.vips_value_get_blob(self.gvalue, psize) buf = ffi.cast('char*', array) result = ffi.unpack(buf, psize[0]) else: raise Error('unsupported gtype for get {0}'.format( type_name(gtype))) return result
def get_blurb(self, name): """Get the blurb for a GObject property.""" c_str = gobject_lib.g_param_spec_get_blurb(self._get_pspec(name)) return _to_string(ffi.string(c_str))
def get_description(self): """Get the description of a GObject.""" vo = ffi.cast('VipsObject *', self.pointer) return _to_string(ffi.string(vips_lib.vips_object_get_description(vo)))