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.
The read() method of the Imgcodecs class is used to read an image using OpenCV.
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2. imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in current working directory.
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 = np.asarray(bytearray(img_stream.read()), dtype=np.uint8) return cv2.imdecode(img_array, cv2_img_flag) def create_opencv_image_from_url(url, cv2_img_flag=0): request = urlopen(url) img_array = np.asarray(bytearray(request.read()), dtype=np.uint8) return cv2.imdecode(img_array, cv2_img_flag)
As pointed out in the comments to the accepted answer, it is outdated and no longer functional.
Luckily, I had to solve this very problem using Python 3.7 with OpenCV 4.0 recently.
To handle image loading from an URL or an in-memory buffer, I defined the following two functions:
import urllib.request import cv2 import numpy as np def get_opencv_img_from_buffer(buffer, flags): bytes_as_np_array = np.frombuffer(buffer.read(), dtype=np.uint8) return cv2.imdecode(bytes_as_np_array, flags) def get_opencv_img_from_url(url, flags): req = urllib.request.Request(url) return get_opencv_img_from_buffer(urllib.request.urlopen(req), flags)
As you can see one depends on the other.
The first one, get_opencv_img_from_buffer, can be used to get an image object from an in-memory buffer. It assumes that the buffer has a read method and that it returns an instance of an object that implements the buffer protocol
The second one, get_opencv_img_from_url, generates an image directly from an URL.
The flags argument is passed on to cv2.imdecode, which has the following constants predefined in cv2:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With