OpenCV 根据 URL 读取网络图片

OpenCV 提供了一个 imread() 函数可以在本地磁盘读取图片并进行各种操作,这里介绍一种方法,可以让OpenCV 根据 URL 也能读取网络图片(配合Nginx服务更好用)

参考链接:【Python】根据 URL 读取网络图片的两种方式(OpenCV)_cv2读取网络图片-CSDN博客

 

调用 VideoCapture 进行转换

imread() 函数虽然不能直接读取网络图片,但 VideoCapture() 可以通过视频的形式进行读取,只需要取其第一帧作为图像即可。

# OpenCV 根据 URL 读取网络图片

import cv2

# 打开视频捕获对象
cap = cv2.VideoCapture("http://192.168.31.166/resource/pic_jiliang/2.jpg")
if not cap.isOpened():
    print("Error: Cannot open video capture.")
    exit()

# 读取图像帧
ret, img = cap.read()
cap.release()

if not ret:
    print("Error: Cannot read image frame.")
    exit()

# 调整图像大小到640x640
resized_img = cv2.resize(img, (1280, 720))

# 显示调整后的图像
cv2.imshow("win", resized_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

posted @ 2024-06-17 09:09  马铃薯1  阅读(30)  评论(0编辑  收藏  举报