Skip to content

Use Rasterio to create webp #23

@vincentsarago

Description

@vincentsarago

in

def _encode_as_webp(data, profile=None, affine=None):
"""
Uses BytesIO + PIL to encode a (3, 512, 512)
array into a webp bytearray.
Parameters
-----------
data: ndarray
(3 x 512 x 512) uint8 RGB array
profile: None
ignored
affine: None
ignored
Returns
--------
contents: bytearray
webp-encoded bytearray of the provided input data
"""
with BytesIO() as f:
im = Image.fromarray(np.rollaxis(data, 0, 3))
im.save(f, format='webp', lossless=True)
return f.getvalue()
we use Pillow to save the image as webp. Since rasterio ~1.0.9 , rasterio wheels are shipped with Webp driver so we could use rasterio instead of PIL and save on the dependency ;-)

cc @dnomadb

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions