I need to process an image (apply filters and other transformation) using python and then serve it to the user using HTTP. Right now, I’m using BaseHTTPServer and PIL.
Problem is, PIL can’t write directly into file streams, so I have to write into a temporary file, and then read this file so I could send it to the user of the service.
Are there any image processing libraries for python that can output JPEG directly to I/O (file-like) streams? is there a way to make PIL do that?
Use the in-memory binary file object
io.BytesIO:This is available on both Python 2 and Python 3, so should be the preferred choice.
On Python 2 only, you can also use the in-memory file object module
StringIO, or it’s faster C-coded equivalentcStringIO:StringIO/cStringIOis the older, legacy implementation of the same principle.