Source code for lightmlrestapi.args.args_images

"""
Playground with images.


:githublink:`%|py|5`
"""
import io
import os
import base64
import numpy
from PIL import Image


[docs]def image2array(img): """ Converts a color imaged into an array. :param img: :epkg:`PIL:Image.Image` :return: :epkg:`numpy:array` :githublink:`%|py|18` """ im_arr = numpy.frombuffer(img.tobytes(), dtype=numpy.uint8) return im_arr.reshape((img.size[1], img.size[0], 3)) # pylint: disable=E1121
[docs]def image2base64(path, fmt='png'): """ Encodes an image into :epkg:`*pyf:base64`. :param path: filename or an image :param fmt: if the image is given as an image (:epkg:`Pillow`), it must be first saved in a specific format (png, jpg, ...) :return: format, base64 The format is the file extension. :githublink:`%|py|33` """ if isinstance(path, bytes): content = path ext = None elif hasattr(path, 'convert'): # Most probably a Pillow object st = io.BytesIO() path.save(st, format=fmt) content = st.getvalue() ext = 'png' else: ext = os.path.splitext(path)[-1].lower().strip('.') with open(path, "rb") as f: content = f.read() return 'image/' + ext, base64.b64encode(content)
[docs]def base642image(encoded): """ Gets an encoded image and builds an :epkg:`PIL:Image.Image` from it. :param encoded: :epkg:`*pyf:base64` encoded image (see :func:`image2base64 <lightmlrestapi.args.args_images.image2base64>`) :return: :epkg:`PIL:Image.Image` :githublink:`%|py|58` """ cont = base64.b64decode(encoded) return Image.open(io.BytesIO(cont))
[docs]def bytes2string(content): """ Converts bytes to string. :githublink:`%|py|66` """ return base64.b64encode(content)
[docs]def string2bytes(content): """ Converts string to bytes. :githublink:`%|py|73` """ return base64.b64decode(content)