예제 #1
0
def vips_image_to_numpy(img: Image) -> np.ndarray:
    """
    https://libvips.github.io/pyvips/intro.html#numpy-and-pil
    """

    np_3d = np.ndarray(buffer=img.write_to_memory(),
                       dtype=FORMAT_TO_DTYPE[img.format],
                       shape=[img.height, img.width, img.bands])
    return np_3d
예제 #2
0
def vips_to_numpy(vips_image: VIPSImage) -> np.ndarray:
    """
    Convert a VIPS image to a Numpy array.

    Parameters
    ----------
    vips_image : VIPSImage
        VIPS image to convert

    Returns
    -------
    image
        Array representation of VIPS image.
        Shape is always (height, width, bands).
    """
    return np.ndarray(
        buffer=vips_image.write_to_memory(),
        dtype=vips_format_to_dtype[vips_image.format],
        shape=[vips_image.height, vips_image.width, vips_image.bands])
예제 #3
0
def vips_image_to_tensor(img: pyvips.Image):
    if img.format == 'uchar':
        tensor = torch.ByteTensor(
            torch.ByteStorage.from_buffer(img.write_to_memory()))
        tensor = tensor.view(img.height, img.width, img.bands)

    else:
        np_img = vips_image_to_numpy(img)

        if np_img.ndim == 2:
            np_img = np_img[:, :, None]

        tensor = torch.from_numpy(np_img)

    tensor = tensor.permute((2, 0, 1)).contiguous()

    if isinstance(tensor, torch.ByteTensor):
        return tensor.float().div(255)
    else:
        return tensor
def to_numpy(vips_image: pyvips.Image) -> np.ndarray:
    return np.ndarray(
        buffer=vips_image.write_to_memory(),
        dtype=FORMAT_MAP[vips_image.format],
        shape=[vips_image.height, vips_image.width, vips_image.bands])