python有哪些库可以读写内存_如何从内存缓冲区(StringIO)或使用opencv python库的url读取映像...

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 = 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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值