How to convert image file object to numpy array in with openCv python?
If you effectively have the contents of a JPEG/PNG file in your variable called response
, I think you can do:
frame = cv2.imdecode(response)
Or, you may need to do:
frame = cv2.imdecode(np.fromstring(response, np.uint8), cv2.IMREAD_COLOR)
Failing that, another way is like this:
from io import BytesIOfrom scipy import miscframe = misc.imread(BytesIO(response))
If the file
is in read-byte mode (which means mode='rb'
) in your case, then numpy.frombuffer()
also works, for example :
file = request.files['fileName']frame = cv2.imdecode(np.frombuffer(file.read(), numpy.uint8), cv2.IMREAD_COLOR)