1

I'm getting uploaded files in my Flask framework app. This file is image which I want to process by OpenCV (in python). Saving this uploaded file to HDD first, will slow down whole operation (additional time of saving and loading image with OpenCV).

Is it possible to load image directly from Werkzeug FileStorage object (memory)?

winnfield
  • 273
  • 4
  • 19

1 Answers1

2

EDIT: I think you might be able to use FileStorage.stream as input to your OpenCV logic—it's a file like object; if that doesn't work, see below. (See werkzeug.datastructures.FileStorage.stream)

Since FileStorage itself doesn't seem to be a file-like object, what you can do is save() it into one:

from cStringIO import StringIO
inmem_file = StringIO()
file_storage.save(inmem_file)  # save to memory
inmem_file.reset()  # seek back to byte 0, otherwise .read() will return ''
use_with_open_cv(inmem_file)

This is assuming OpenCV can work with arbitrary file-like objects, not just objects representing to real files.

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
  • Thanks @ErikAllik! I used code posted in question: http://stackoverflow.com/questions/11552926/how-to-read-raw-png-from-an-array-in-python-opencv by @NoamGeffen. – winnfield Nov 01 '13 at 23:34