赞
踩
pil
- 方法二,使用PIL+requests:
-
- import requests
- from PIL import Image
- from io import BytesIO
-
- response = requests.get(img_src)
- image = Image.open(BytesIO(response.content))
- image.save('D:/9.jpg')
-
- import time
-
- import cv2
- import numpy as np
- import requests
-
- path='d:/guo.jpg'
-
- # img=cv2.imread(path)
- # x = img.tobytes()
- # # 从二进制文件到图片(numpy.ndarray):
- #
- # aaa=np.fromstring(x, np.uint8)
- # img = cv2.imdecode(aaa ,cv2.IMREAD_COLOR)
- #
- # cv2.imshow("1",img)
- # cv2.waitKeyEx()
- # import cv2 # opencv-python (3.4.2.16)
- # import numpy as np # numpy (1.14.5)
-
-
- #每一张图片需要600ms
- for i in range(10):
- start=time.time()
- file = requests.get("https://www.baidu.com/img/bd_logo1.png")
- img = cv2.imdecode(np.fromstring(file.content, np.uint8), 1) #file.content 是读取的远程文件的字节流
- print('time',time.time()-start)
- cv2.imshow("1",img)
- cv2.waitKeyEx()
-
-
- #需要1000ms左右
- for i in range(10):
- start=time.time()
- # file = requests.get("https://www.baidu.com/img/bd_logo1.png")
- url = 'http://i5.qhimg.com/t019c3e49c9c9319c33.jpg'
- url = 'https://www.baidu.com/img/bd_logo1.png'
- cap = cv2.VideoCapture(url)
- ret = cap.isOpened()
- while (ret):
- ret, img = cap.read()
- if not ret: break
- # img = cv2.imdecode(np.fromstring(file.content, np.uint8), 1) #file.content 是读取的远程文件的字节流
- print('time',time.time()-start)
- cv2.imshow('photo', img)
- cv2.waitKey(0)
c++读取:
https://answers.opencv.org/question/91344/load-image-from-url/
- #include "curl/curl.h" // has to go before opencv headers
-
- #include <iostream>
- #include <vector>
- using namespace std;
-
- #include <opencv2/opencv.hpp>
- using namespace cv;
-
- //curl writefunction to be passed as a parameter
- // we can't ever expect to get the whole image in one piece,
- // every router / hub is entitled to fragment it into parts
- // (like 1-8k at a time),
- // so insert the part at the end of our stream.
- size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata)
- {
- vector<uchar> *stream = (vector<uchar>*)userdata;
- size_t count = size * nmemb;
- stream->insert(stream->end(), ptr, ptr + count);
- return count;
- }
-
- //function to retrieve the image as cv::Mat data type
- cv::Mat curlImg(const char *img_url, int timeout=10)
- {
- vector<uchar> stream;
- CURL *curl = curl_easy_init();
- curl_easy_setopt(curl, CURLOPT_URL, img_url); //the img url
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); // pass the writefunction
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream); // pass the stream ptr to the writefunction
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); // timeout if curl_easy hangs,
- CURLcode res = curl_easy_perform(curl); // start curl
- curl_easy_cleanup(curl); // cleanup
- return imdecode(stream, -1); // 'keep-as-is'
- }
-
- int main(void)
- {
- Mat image = curlImg("http://www.cars.co.za/images/pictures/general/graphic_sellyourcar.png");
- if (image.empty())
- return -1; // load fail
-
- namedWindow( "Image output", CV_WINDOW_AUTOSIZE );
- imshow("Image output",image); // here's your car ;)
- waitKey(0); // infinite
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。