Home > Backend Development > Python Tutorial > Detailed explanation of how Python opens an image through a URL

Detailed explanation of how Python opens an image through a URL

黄舟
Release: 2017-06-04 10:16:16
Original
2281 people have browsed it

This article mainly introduces the relevant information of Python opening pictures through URL. Friends in need can refer to

Python through URL Detailed explanation of opening image examples

Whether you use OpenCV or PIL, skimage and other libraries, when doing image processing before, you almost always read local images. I recently tried crawling pictures. Before saving, I wanted to quickly browse the pictures and then save them selectively. Here you need to read the image from the url. I checked a lot of information and found that there are several methods. I will make a record here.

The image URL used in this article is as follows:

img_src = 'http://wx2.sinaimg.cn/mw690/ac38503ely1fesz8m0ov6j20qo140dix.jpg'
Copy after login

1. Using OpenCV

OpenCV’s imread() can only load local ones Pictures cannot be loaded through URLs. However, opencv's VideoCapture class can load videos from url. If we only use opencv, we can use a roundabout way: first use VideoCapure to load the image under the URL, and then pass it to Mat.

import cv2
cap = cv2.VideoCapture(img_src)
if( cap.isOpened() ) :
  ret,img = cap.read()
  cv2.imshow("image",img)
  cv2.waitKey()
Copy after login

2. OpenCV+Numpy+urllib

import numpy as np
import urllib
import cv2
resp = urllib.urlopen(img_src)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
cv2.imshow("Image", image)
cv2.waitKey(0)
Copy after login

urlopen returns a class file object , which provides the following methods:

read() , readline() , readlines() , fileno() , close() : These methods are used exactly like file objects. Then re-encode the returned file-like object, convert it into a picture and pass it to Mat.

3.PIL+requests

import requests as req
from PIL import Image
from io import BytesIO
response = req.get(img_src)
image = Image.open(BytesIO(response.content))
image.show()
Copy after login

requests can access the request response body in bytes. The above is the code to create a picture with the binary data returned by the request. .

4. skimage

from skimage import io
image = io.imread(img_src)
io.imshow(image)
io.show()
Copy after login

Relatively speaking, this method should be the simplest, because skimage can directly use imread() function To read web page images without any other assistance or detours.

The above is the detailed content of Detailed explanation of how Python opens an image through a URL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template