Make it possible to load image from a buffer
inikolaev opened this issue · 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)
you could use the io
python standard library to "fake" a file stream.
import io
import lycon
image = lycon.load(io.BytesIO(buffer))
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
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.