Example #1
0
def gdkpixbuf_loader(file_obj, string):
    """Load raster images with GDK-PixBuf."""
    if pixbuf is None:
        raise OSError(
            'Could not load GDK-Pixbuf. '
            'PNG and SVG are the only image formats available.')
    if string is None:
        string = file_obj.read()
    surface, format_name = pixbuf.decode_to_image_surface(string)
    if format_name == 'jpeg':
        surface.set_mime_data('image/jpeg', string)
    get_pattern = lambda: cairocffi.SurfacePattern(surface)
    return get_pattern, surface.get_width(), surface.get_height()
Example #2
0
def surface_to_npim(surface):
    """ Transforms a Cairo surface into a numpy array. """
    im = +np.frombuffer(surface.get_data(), np.uint8)
    H,W = surface.get_height(), surface.get_width()
    im.shape = (H,W, 4) # for RGBA
    return im[:,:,:3]
Example #3
0
def cairo_png_loader(file_obj, string):
    """Return a cairo Surface from a PNG byte stream."""
    surface = cairocffi.ImageSurface.create_from_png(
        file_obj or BytesIO(string))
    get_pattern = lambda: cairocffi.SurfacePattern(surface)
    return get_pattern, surface.get_width(), surface.get_height()