문제

파이썬 CV2의 인터넷 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())
. 파이썬이 나에게보고 되었기 때문에

는 좋지 않습니다.

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

도움이 되었습니까?

해결책

CV2 이미지는 문자열 (유니 코드 하나, YUCC 저장)이 아니라 NUMPY 배열, - CV2 및 숫자를 사용하여 다음을 달성합니다.

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()
.

다른 팁

다음은 이미지를 숫자로 직접 읽습니다.

from skimage import io

image = io.imread('https://raw2.github.com/scikit-image/scikit-image.github.com/master/_static/img/logo.png')
.

python3 :

from urllib.request import urlopen
def url_to_image(url, readFlag=cv2.IMREAD_COLOR):
    # download the image, convert it to a NumPy array, and then read
    # it into OpenCV format
    resp = urlopen(url)
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    image = cv2.imdecode(image, readFlag)

    # return the image
    return image
.

이것은 imutils에서 URL_TO_Image의 구현이므로

를 호출 할 수 있습니다.
import imutils
imutils.url_to_image(url)
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top