def new_from_memory(data): """Make a new source from a memory object. Make a new source that is attached to the memory object. For example:: source = pyvips.Source.new_from_memory("myfile.jpg") You can pass this source to (for example) :meth:`new_from_source`. The memory object can be anything that supports the Python buffer or memoryview protocol. """ # logger.debug('VipsSource.new_from_memory:') # py3: # - memoryview has .nbytes for number of bytes in object # - len() returns number of elements in top array # py2: # - buffer has no nbytes member # - but len() gives number of bytes in object start = ffi.from_buffer(data) nbytes = data.nbytes if hasattr(data, 'nbytes') else len(data) pointer = vips_lib.vips_source_new_from_memory(start, nbytes) if pointer == ffi.NULL: raise Error("can't create input source from memory") source = Source(pointer) # keep a secret reference to the input data to make sure it's not GCed source._references = [data] return source
def new_from_memory(data, width, height, bands, format): """Wrap an image around a memory array. Wraps an Image around an area of memory containing a C-style array. For example, if the ``data`` memory array contains four bytes with the values 1, 2, 3, 4, you can make a one-band, 2x2 uchar image from it like this:: image = Image.new_from_memory(data, 2, 2, 1, 'uchar') A reference is kept to the data object, so it will not be garbage-collected until the returned image is garbage-collected. This method is useful for efficiently transferring images from PIL or NumPy into libvips. See :meth:`.write_to_memory` for the opposite operation. Use :meth:`.copy` to set other image attributes. Args: data (bytes): A memoryview or buffer object. width (int): Image width in pixels. height (int): Image height in pixels. bands (int): Number of bands. format (BandFormat): Band format. Returns: A new :class:`Image`. Raises: :class:`.Error` """ format_value = GValue.to_enum(GValue.format_type, format) pointer = ffi.from_buffer(data) # py3: # - memoryview has .nbytes for number of bytes in object # - len() returns number of elements in top array # py2: # - buffer has no nbytes member # - but len() gives number of bytes in object nbytes = data.nbytes if hasattr(data, 'nbytes') else len(data) vi = vips_lib.vips_image_new_from_memory(pointer, nbytes, width, height, bands, format_value) if vi == ffi.NULL: raise Error('unable to make image from memory') image = pyvips.Image(vi) # keep a secret ref to the underlying object .. this reference will be # inherited by things that in turn depend on us, so the memory we are # using will not be freed image._references.append(data) return image
def new_from_memory(data, width, height, bands, format): """Wrap an image around a memory array. Wraps an Image around an area of memory containing a C-style array. For example, if the ``data`` memory array contains four bytes with the values 1, 2, 3, 4, you can make a one-band, 2x2 uchar image from it like this:: image = Image.new_from_memory(data, 2, 2, 1, 'uchar') A reference is kept to the data object, so it will not be garbage-collected until the returned image is garbage-collected. This method is useful for efficiently transferring images from PIL or NumPy into libvips. See :meth:`.write_to_memory` for the opposite operation. Use :meth:`.copy` to set other image attributes. Args: data (bytes): A memoryview or buffer object. width (int): Image width in pixels. height (int): Image height in pixels. format (BandFormat): Band format. Returns: A new :class:`Image`. Raises: :class:`.Error` """ format_value = GValue.to_enum(GValue.format_type, format) vi = vips_lib.vips_image_new_from_memory(ffi.from_buffer(data), len(data), width, height, bands, format_value) if vi == ffi.NULL: raise Error('unable to make image from memory') image = pyvips.Image(vi) # keep a secret ref to the underlying object image._data = data return image