Opencv之通过url抓取图片并通过opencv可视化

代码展示

import cv2
import numpy as np
import urllib.request


def fetchImageFromHttp(image_url, timeout_s=1):
    try:
        if image_url:
            resp = urllib.request.urlopen(image_url, timeout=timeout_s)
            image = np.asarray(bytearray(resp.read()), dtype="uint8")
            image = cv2.imdecode(image, cv2.IMREAD_COLOR)
            return image
        else:
            return []
    except Exception as error:
        print('获取图片失败', error)
        return []


img = fetchImageFromHttp(
    'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3363295869,2467511306&fm=26&gp=0.jpg')
cv2.imshow('img', img)
cv2.waitKey()

效果展示

Opencv之通过url抓取图片并通过opencv可视化_第1张图片

你可能感兴趣的:(opencv)