How to read image from in memory buffer (StringIO) or from url with opencv python library
Just share a way to create opencv image object from in memory buffer or from url to improve performance. Sometimes we get image binary from url, to avoid additional file IO, we want to imread this image from in memory buffer or from url, but imread only supports read image from file system with path. To create an OpenCV image object with in memory buffer(StringIO), we can use OpenCV API imdecode, see code below: import cv2 import numpy as np from urllib2 import urlopen from cStringIO import StringIO def create_opencv_image_from_stringio(img_stream, cv2_img_flag=0): img_stream.seek(0) img_array