Convert URL to image with Python and OpenCV(根据URL下载图片)
2017-02-13 19:21
621 查看
http://www.pyimagesearch.com/2015/03/02/convert-url-to-image-with-python-and-opencv/
And as a bonus we’ll also see how we can utilize scikit-image to
download an image from a URL, along with a common “gotcha” that could trip you up along the way.
OpenCV and Python versions:
In order to run this example, you’ll need Python 2.7 and OpenCV
2.4.X.
The first method we’ll explore is converting a URL to an image using the OpenCV, NumPy, and the urllib libraries. Open up a new file, name it url_to_image.py ,
and let’s get started:
# import the necessary packages
import numpy as np
import urllib
import cv2
# METHOD #1: OpenCV, NumPy, and urllib
def url_to_image(url):
# download the image, convert it to a NumPy array, and then read
# it into OpenCV format
resp = urllib.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
# return the image
return image
The first thing we’ll do is import our necessary packages. We’ll use NumPy for converting the byte-sequence from the download to a NumPy array, urllib to
perform the actual request, and cv2 for
our OpenCV bindings.
We then define our url_to_image function
on Line 7. This function requires a single argument, url ,
which is the URL of the image we want to download.
Next, we utilize the urllib library
to open a connection to the supplied URL on Line 10. The raw byte-sequence from the request is then converted to a NumPy array on Line
11.
At this point the NumPy array is a 1-dimensional array (i.e. a long list of pixels). To reshape the array into a 2D format, assuming 3 components per pixel (i.e. the Red, Green, and Blue components, respectively), we make a call to cv2.imdecode on Line
12. Finally, we return the decoded image to the calling function on Line 15.
Lines 18-21 define a list of image URLs that we are going to download and convert to OpenCV format.
We start looping over each of these URLs on Line 25, make a call to oururl_to_image function
on Line 28, and then finally display our downloaded image to our screen on Lines
29 and 30. At this point our image can be manipulated with any other OpenCV functions as we normally would.
To see our work in action, open up a terminal and execute the following command:
The second method assumes that you have the scikit-image library
installed on your system. Let’s take a look at how we can leverage scikit-image to download an image from a URL and convert it to OpenCV format:
# METHOD #2: scikit-image
from skimage import io
# loop over the image URLs
for url in urls:
# download the image using scikit-image
print "downloading %s" % (url)
image = io.imread(url)
cv2.imshow("Incorrect", image)
cv2.imshow("Correct", cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
cv2.waitKey(0)
And as a bonus we’ll also see how we can utilize scikit-image to
download an image from a URL, along with a common “gotcha” that could trip you up along the way.
OpenCV and Python versions:
In order to run this example, you’ll need Python 2.7 and OpenCV
2.4.X.
Method #1: OpenCV, NumPy, and urllib
The first method we’ll explore is converting a URL to an image using the OpenCV, NumPy, and the urllib libraries. Open up a new file, name it url_to_image.py ,and let’s get started:
# import the necessary packages
import numpy as np
import urllib
import cv2
# METHOD #1: OpenCV, NumPy, and urllib
def url_to_image(url):
# download the image, convert it to a NumPy array, and then read
# it into OpenCV format
resp = urllib.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
# return the image
return image
The first thing we’ll do is import our necessary packages. We’ll use NumPy for converting the byte-sequence from the download to a NumPy array, urllib to
perform the actual request, and cv2 for
our OpenCV bindings.
We then define our url_to_image function
on Line 7. This function requires a single argument, url ,
which is the URL of the image we want to download.
Next, we utilize the urllib library
to open a connection to the supplied URL on Line 10. The raw byte-sequence from the request is then converted to a NumPy array on Line
11.
At this point the NumPy array is a 1-dimensional array (i.e. a long list of pixels). To reshape the array into a 2D format, assuming 3 components per pixel (i.e. the Red, Green, and Blue components, respectively), we make a call to cv2.imdecode on Line
12. Finally, we return the decoded image to the calling function on Line 15.
# initialize the list of image URLs to download urls = [ "http://www.pyimagesearch.com/wp-content/uploads/2015/01/opencv_logo.png", "http://www.pyimagesearch.com/wp-content/uploads/2015/01/google_logo.png", "http://www.pyimagesearch.com/wp-content/uploads/2014/12/adrian_face_detection_sidebar.png", ] # loop over the image URLs for url in urls: # download the image URL and display it print "downloading %s" % (url) image = url_to_image(url) cv2.imshow("Image", image) cv2.waitKey(0)
Lines 18-21 define a list of image URLs that we are going to download and convert to OpenCV format.
We start looping over each of these URLs on Line 25, make a call to oururl_to_image function
on Line 28, and then finally display our downloaded image to our screen on Lines
29 and 30. At this point our image can be manipulated with any other OpenCV functions as we normally would.
To see our work in action, open up a terminal and execute the following command:
Method #2: scikit-image
The second method assumes that you have the scikit-image libraryinstalled on your system. Let’s take a look at how we can leverage scikit-image to download an image from a URL and convert it to OpenCV format:
# METHOD #2: scikit-image
from skimage import io
# loop over the image URLs
for url in urls:
# download the image using scikit-image
print "downloading %s" % (url)
image = io.imread(url)
cv2.imshow("Incorrect", image)
cv2.imshow("Correct", cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
cv2.waitKey(0)
相关文章推荐
- 用Python和OpenCV创建一个图片搜索引擎的完整指南 The complete guide to building an image search engine with Python and
- 根据url下载一张大小为1M左右的图片,然后用个ImageView显示出来。不爆内存的哦!!!
- IOS开发-UI学习-根据URL显示图片,下载图片的练习(button,textfield,image view,url,data)
- 用 Python 和 OpenCV 检测图片上的条形码Detecting Barcodes in Images with Python and OpenCV
- 流输出图片到JSP页面:Image to Byte Array and Byte Array to Image
- Vision with Direction: A Systematic Introduction to Image Processing and Computer Vision
- Image Reflection with jQuery and MooTools Example实现图片半透明渐变倒影效果
- loadrunner 根据图片url下载图片并重命名。
- python下载文件(图片)源码,包含爬网内容(爬url),可保存cookie
- Beginning Game Development with Python and Pygame: From Novice to Professional
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- 关于URLDownlaodtoFile()函数下载的验证码图片都一样的问题的解决
- 图片不能显示: [cell.imageView setImageWithURL:imageUrl placeholderImage:nil options:SDWebImageLowPriority
- How to scan images, lookup tables and time measurement with OpenCV
- [UIImageView setImageWithURL:]: unrecognized selector sent to instance解决办法
- IE title changes to <afterHash> if the page has a url with '#' , and has flash/swf embedded in it
- 根据根据图片的url怎么取得图片ImageView对象
- python小练——下载指定url中的图片
- [zz]Scripting KVM with Python, Part 2: Add a GUI to manage KVM with libvirt and Python
- Java根据Url下载图片