Skip to content
Advertisement

is there a way to manipulate image without saving to a file first?

I’m trying to collect img data for my ML/DL project.

I need facial data so, I have to detect the face and crop around it.

I have a bunch of img URLs that I searched online.

Normally I would save them in a file using requests library, but would it be possible to do it in-memory?

JavaScript

I looked at some image libraries in python such as PIL or OpenCV,

but they all seems to get images loaded from a file in first place.

I think I could speed up the process if I don’t save temporary files. (I/O is a big bottleneck)

I tried playing around with BytesIO functions but I couldn’t figure it out.

Advertisement

Answer

BytesIO is the way to go!. You can store the binary data returned by the request to the In-memory byte buffer(BytesIO) and pass it to Image.open

JavaScript

The only requirement of the Image.open API is that the fp argument must be a filename (string), pathlib.Path object or a file object. The file object must implement file.read, file.seek, and file.tell methods, and be opened in binary mode.

BytesIO implements all these methods.

JavaScript
Advertisement