作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
如何在 Python cv2 中从 Internet URL 读取图像?
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/
我是一名优秀的程序员,十分优秀!