Image Helpers

Links: notebook, html, PDF, python, slides, slides(2), GitHub

Sending an image to a REST API is about choosing a convention. Here are some helpers.

from jyquickhelper import add_notebook_menu
add_notebook_menu()

base64

The usual format to send an image is base64.

from lightmlrestapi.testing.data import get_wiki_img
imgpath = get_wiki_img()
from IPython.display import Image
Image(imgpath, width=200)
../_images/image_helpers_4_0.png
from PIL import Image
img = Image.open(imgpath)
img.size
(456, 415)
from lightmlrestapi.args import image2base64
ext, b64 = image2base64(img)
len(b64), type(b64)
(114676, bytes)
from lightmlrestapi.args import base642image
img2 = base642image(b64)
img2.resize((150,150))
../_images/image_helpers_8_0.png

array

from lightmlrestapi.args import image2array
arr = image2array(img)
arr.shape
(415, 456, 3)