Today's blog is directly derived from my own personal tool library.
Over the past few months, some pyimagesearch readers have emailed me: "How do I get the picture that the URL points to and convert it to the OPENCV format without having to write it to disk and then read back?" In this article I will show you how to implement this function.
Additional, we will also see how to use Scikit-image to download an image from the URL. Of course there will be a common mistake on the way forward, which may make you stumble.
Continue reading to learn how to use Python and OpenCV to convert URLs to images
methods 1:opencv, NumPy, Urllib
The first method: we use the OpenCV, NumPy, Urllib Library to get the image from the URL and convert it to an image. Open and create a new file, named url_to_image.py, let's get started:
# import the necessary packages
import NumPy as NP
import urllib
import cv2
# method #1: OpenCV, NumPy, an D urllib
def url_to_image (URL):
# Download the image, convert it to a numpy array, and then read
# it into Ope NCV format
resp = urllib.urlopen (URL)
image = Np.asarray (ByteArray ()), resp.read "dtype=")
image = Cv2.imdecode (image, Cv2. Imread_color)
# return the image return
image
The first thing to do is to import the packages we need. We will use the NumPy transform to download the byte order as the NumPy array, use Urllib to execute the actual network request, use the CV2 to bind the OpenCV interface.
In line 7th, we define our url_to_image function. This function takes a URL parameter, which is the image address we want to download.
Next, on line 10th, we use the Urllib library to open this image link. Line 11 converts the downloaded byte sequence to a numpy array.
At this point, the NumPy array is also a 1-D array (that is, a long pixel list). To convert it to a 2-D format, suppose 3 channels per pixel (meaning red, green, blue channel respectively), we use the Cv.imdecode function in line 12. Finally, in line 15 we return the decoded image to the calling function.
Everything is ready, it's time to let it work:
# 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)
Line 3-5 defines the list of image addresses that we will download and convert to OPENCV format.
Line 9th We traverse this list, and 13 lines call the Url_to_image function, and then the images that are fetched in lines 14 and 15 are displayed to the screen. In this case, we can use OPENCV to manipulate and manipulate these images as we normally do.
Seeing as reality, open the terminal, execute the following instructions:
Copy Code code as follows:
If all goes well, you will see the logo of OpenCV:
Figure 1: Download OpenCV logo from URL and convert to OPENCV format
Next is Google's logo:
Figure 2: Download Gooogle from URL and convert to OPENCV format
There are also examples of human face detection in my book, "Practical Python and OpenCV":
Figure 3: Converting a URL image to OPENCV format
Now let's look at another way to get the image and convert it to the OPENCV format.
Method 2: Use Scikit-image
The second method assumes that you have installed the Scikit-image library on your computer. Let's see how to get the image from the URL and convert it to the OPENCV format using scikit-image:
# method #2: Scikit-image from
skimage import io
# loops over the image URLs to
URLs in URLs:
# download th E 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)
A nice thing to do in the Scikit-image library is that the Imread function in the IO Sub Library distinguishes the image path from the disk or the URL (line 9th).
In spite of this, there is a serious mistake here that may make you fall a somersault!
OpenCV expresses an image in BGR order, whereas Scikit-image is an RGB order. If you use the Scikit-iamge imread function, and you want to use the OpenCV function after the download is complete, be careful. As described in line 41, you need to convert the image from RBG to BGR.
If you do not have this step, then you may get the wrong result:
Figure 4: When using Scikit-image, special attention is needed to convert RGB to BGR. The image on the left is not the correct RGB order, the right is to convert RGB to BGR, so the normal display.
Look at Google's logo, it's more obvious.
Figure 5: The order is important. Make sure to convert RGB to BGR or leave a bug that is hard to find.
That's it, you get it! These two methods use Python, OpenCV, Urllib, and scikit-image to convert a picture that the URL points to an image.
Summary
In this article, we learned how to get an image from a URL and convert it to OPENCV format using Python and OpenCV.
The first method uses the Urllib package to get the image, converts the array using NumPy, and finally uses OPENCV to reconstruct the arrays to produce our images.
The second way is to use the Io.imread function in Scikit-image.
So which is better?
It all depends on your installation.
If you've already installed Scikit-image, then I'll probably use io.imread (just don't forget to convert RGB to BGR If you want to use the OPENCV function).
If you don't have Scikit-image installed, then Url_to_image is a handy tool. The details refer to the beginning of this article.
I will soon add this function to the Imutils library in GitHub.