python opencv读取网络图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jacke121/article/details/88121696
import time

import cv2
import numpy as np
import requests

path='d:/guo.jpg'

# img=cv2.imread(path)
# x = img.tobytes()
# # 从二进制文件到图片(numpy.ndarray):
#
# aaa=np.fromstring(x, np.uint8)
# img = cv2.imdecode(aaa ,cv2.IMREAD_COLOR)
#
# cv2.imshow("1",img)
# cv2.waitKeyEx()
# import cv2   # opencv-python (3.4.2.16)
# import numpy as np  # numpy (1.14.5)


#每一张图片需要600ms
for i in range(10):
    start=time.time()
    file = requests.get("https://www.baidu.com/img/bd_logo1.png")
    img = cv2.imdecode(np.fromstring(file.content, np.uint8), 1)    #file.content 是读取的远程文件的字节流
    print('time',time.time()-start)
cv2.imshow("1",img)
cv2.waitKeyEx()


#需要1000ms左右
for i in range(10):
    start=time.time()
    # file = requests.get("https://www.baidu.com/img/bd_logo1.png")
    url = 'http://i5.qhimg.com/t019c3e49c9c9319c33.jpg'
    url = 'https://www.baidu.com/img/bd_logo1.png'
    cap = cv2.VideoCapture(url)
    ret = cap.isOpened()
    while (ret):
        ret, img = cap.read()
        if not ret: break
    # img = cv2.imdecode(np.fromstring(file.content, np.uint8), 1)    #file.content 是读取的远程文件的字节流
    print('time',time.time()-start)
cv2.imshow('photo', img)
cv2.waitKey(0)

猜你喜欢

转载自blog.csdn.net/jacke121/article/details/88121696
今日推荐