.. _imagehelpersrst: ============= Image Helpers ============= .. only:: html **Links:** :download:`notebook `, :downloadlink:`html `, :download:`PDF `, :download:`python `, :downloadlink:`slides `, :githublink:`GitHub|_doc/notebooks/image_helpers.ipynb|*` Sending an image to a REST API is about choosing a convention. Here are some helpers. .. code:: ipython3 from jyquickhelper import add_notebook_menu add_notebook_menu() .. contents:: :local: base64 ------ The usual format to send an image is `base64 `__. .. code:: ipython3 from lightmlrestapi.testing.data import get_wiki_img imgpath = get_wiki_img() .. code:: ipython3 from IPython.display import Image Image(imgpath, width=200) .. image:: image_helpers_4_0.png :width: 200px .. code:: ipython3 from PIL import Image img = Image.open(imgpath) img.size .. parsed-literal:: (456, 415) .. code:: ipython3 from lightmlrestapi.args import image2base64 ext, b64 = image2base64(img) len(b64), type(b64) .. parsed-literal:: (114676, bytes) .. code:: ipython3 from lightmlrestapi.args import base642image img2 = base642image(b64) .. code:: ipython3 img2.resize((150,150)) .. image:: image_helpers_8_0.png array ----- .. code:: ipython3 from lightmlrestapi.args import image2array arr = image2array(img) arr.shape .. parsed-literal:: (415, 456, 3)