Source code for libertem.common.viz

from io import BytesIO
from typing import Dict, Optional

from PIL import Image

[docs]def encode_image(result, save_kwargs: Optional[Dict] = None) -> BytesIO: """ Save the RGBA data in ``result`` to an image with parameters ``save_kwargs`` passed to ````. Parameters ---------- result : numpy.ndarray Array of RGB values; shape (height, width, 3) save_kwargs : dict or None dict of kwargs passed to Pillow when saving the image, can be used to set the file format, quality, ... Returns ------- BytesIO a buffer containing the result image (as PNG/JPG/... depending on save_kwargs) """ if save_kwargs is None: save_kwargs = {'format': 'png'} # see also: im = Image.fromarray(result) buf = BytesIO() im = im.convert(mode="RGB"), **save_kwargs) return buf