lycon icon indicating copy to clipboard operation
lycon copied to clipboard

Make it possible to load image from a buffer

Open inikolaev opened this issue 7 years ago • 3 comments

Right now it's only possible to load image from a file. I have a use case where I would like to load image from a buffer, specifically the the response contents returned by requests library, e.g.:

import lycon
import requests

response = requests.get("https://example.com/image.jpeg")
image = lycon.load(response.content)

inikolaev avatar May 03 '18 20:05 inikolaev

you could use the io python standard library to "fake" a file stream.

import io
import lycon
image = lycon.load(io.BytesIO(buffer))

l3robot avatar Jul 04 '18 18:07 l3robot

Using a io.BytesIO() didn't work for me:

    return lycon.load(io.BytesIO(png_dat))

_lycon.PyconError: Assertion Failure: `str` evaluated to false in `string_from_pyobject` (/tmp/pip-install-nv5q896g/lycon/src/lycon/python/interop.cc:322)

A gross work around is to write to a temp file and then read from that:

    (fdesc, fname) = tempfile.mkstemp(prefix='tmp-lycon-hack-')
    os.write(fdesc, png_dat)
    os.close(fdesc)

    dat = lycon.load(fname)
    os.remove(fname)
    return dat

nieksand avatar Nov 13 '18 15:11 nieksand

It looks from reading the source that Lycon really only supports passing a file name argument to lycon.load(). This makes it impossible to load images from container formats such as a zipfile using lycon and various other uses. I was excited to learn about a faster library than PIL.Image, but the API is a dealbreaker for me.

nurpax avatar Jan 09 '21 13:01 nurpax