如何在Python cv2,scikit图像和mahotas中从Internet URL读取图像? [英] How can I read an image from an Internet URL in Python cv2, scikit image and mahotas?

查看:117
本文介绍了如何在Python cv2,scikit图像和mahotas中从Internet URL读取图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I read an image from an Internet URL in Python cv2?

堆栈溢出答案

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向我报告了:

is not good because Python reported to me:

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

推荐答案

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

Since a cv2 image is not a string (save a Unicode one, yucc), but a NumPy array, - use cv2 and NumPy to achieve it:

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 cv2,scikit图像和mahotas中从Internet URL读取图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆