Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# wrap VipsImage
from __future__ import division
import numbers
import pyvips
from pyvips import ffi, glib_lib, vips_lib, Error, _to_bytes, \
_to_string, GValue
ffi.cdef('''
typedef struct _VipsImage {
VipsObject parent_instance;
// opaque
} VipsImage;
const char* vips_foreign_find_load (const char* name);
const char* vips_foreign_find_load_buffer (const void* data, size_t size);
const char* vips_foreign_find_save (const char* name);
const char* vips_foreign_find_save_buffer (const char* suffix);
VipsImage* vips_image_new_matrix_from_array (int width, int height,
const double* array, int size);
VipsImage* vips_image_new_from_memory (const void* data, size_t size,
int width, int height, int bands, int format);
Make a new stream that is attached to the descriptor. For example::
streamo = pyvips.Streamo.new_to_descriptor(1)
Makes a descriptor attached to stdout.
You can pass this stream to (for example) :meth:`write_to_stream`.
"""
# logger.debug('VipsStreamo.new_to_descriptor: descriptor = %d',
# descriptor)
# streams are mutable, so we can't use the cache
pointer = vips_lib.vips_streamo_new_to_descriptor(descriptor)
if pointer == ffi.NULL:
raise Error("can't create output stream from descriptor {0}"
.format(descriptor))
return Streamo(pointer)
def _get_pspec(self, name):
# logger.debug('VipsObject.get_typeof: self = %s, name = %s',
# str(self), name)
pspec = ffi.new('GParamSpec **')
argument_class = ffi.new('VipsArgumentClass **')
argument_instance = ffi.new('VipsArgumentInstance **')
result = vips_lib.vips_object_get_argument(self.vobject, _to_bytes(name),
pspec, argument_class,
argument_instance)
if result != 0:
return None
return pspec[0]
Make a new stream that is attached to the descriptor. For example::
streami = pyvips.Streami.new_from_descriptor(0)
Makes a descriptor attached to stdin.
You can pass this stream to (for example) :meth:`new_from_stream`.
"""
# logger.debug('VipsStreami.new_from_descriptor: descriptor = %d',
# descriptor)
# streams are mutable, so we can't use the cache
pointer = vips_lib.vips_streami_new_from_descriptor(descriptor)
if pointer == ffi.NULL:
raise Error("can't create input stream from descriptor {0}"
.format(descriptor))
return Streami(pointer)
"""
if not _is_2D(array):
array = [array]
height = len(array)
width = len(array[0])
n = width * height
a = ffi.new('double[]', n)
for y in range(0, height):
for x in range(0, width):
a[x + y * width] = array[y][x]
vi = vips_lib.vips_image_new_matrix_from_array(width, height, a, n)
if vi == ffi.NULL:
raise Error('unable to make image from matrix')
image = pyvips.Image(vi)
image.set_type(GValue.gdouble_type, 'scale', scale)
image.set_type(GValue.gdouble_type, 'offset', offset)
return image
p_names, p_flags, p_n_args)
if result != 0:
raise Error('unable to get arguments from operation')
p_names = p_names[0]
p_flags = p_flags[0]
n_args = p_n_args[0]
for i in range(0, n_args):
add_args(_to_string(p_names[i]), p_flags[i])
else:
def add_construct(self, pspec, argument_class,
argument_instance, a, b):
add_args(_to_string(pspec.name), argument_class.flags)
return ffi.NULL
cb = ffi.callback('VipsArgumentMapFn', add_construct)
vips_lib.vips_argument_map(op.vobject, cb, ffi.NULL, ffi.NULL)
# logger.debug('arguments = %s', self.arguments)
# build a hash from arg name to detailed arg information
self.details = {}
for name, flags in arguments:
self.details[name] = {
"name": name,
"flags": flags,
"blurb": op.get_blurb(name),
"type": op.get_typeof(name)
}
# lists of arg names by category
self.required_input = []
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 __init__(self):
# allocate memory for the gvalue which will be freed on GC
self.pointer = ffi.new('GValue *')
# logger.debug('GValue.__init__: pointer = %s', self.pointer)
# and tag it to be unset on GC as well
self.gvalue = ffi.gc(self.pointer, gobject_lib.g_value_unset)
# logger.debug('GValue.__init__: gvalue = %s', self.gvalue)
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)