gpt4 book ai didi

python - 如何从 Python cv2、scikit 图像和 mahotas 中的 Internet URL 读取图像?

转载 作者:IT老高 更新时间:2023-10-28 20:44:14 26 4
gpt4 key购买 nike

如何在 Python cv2 中从 Internet URL 读取图像?

这个 Stack Overflow answer ,

import cv2.cv as cv
import urllib2
from cStringIO import StringIO
import PIL.Image as pil
url="some_url"

img_file = urllib2.urlopen(url)
im = StringIO(img_file.read())

不好,因为Python向我报告了:

TypeError: object.__new__(cStringIO.StringI) is not safe, use cStringIO.StringI.__new__

最佳答案

由于 cv2 图像不是字符串(保存一个 Unicode 的,yucc),而是一个 NumPy 数组, - 使用 cv2 和 NumPy 来实现它:

import cv2
import urllib
import numpy as np

req = urllib.urlopen('http://answers.opencv.org/upfiles/logo_2.png')
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr, -1) # 'Load it as it is'

cv2.imshow('lalala', img)
if cv2.waitKey() & 0xff == 27: quit()

关于python - 如何从 Python cv2、scikit 图像和 mahotas 中的 Internet URL 读取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21061814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com