0
点赞
收藏
分享

微信扫一扫

Python获取网络图片扩展名、大小


PIL实现

安装Pillow:

pip install Pillow

import requests
from PIL import Image
from io import BytesIO

img_url = 'https://img-home.csdnimg.cn/images/20201124032511.png'

req_img = requests.get(img_url).content
img = Image.open(BytesIO(req_img))
print('图片格式:', img.format)
print('图片大小:', img.size)

Python获取网络图片扩展名、大小_html

OpenCV

安装opencv-python:

pip install opencv-python

import numpy as np
import cv2
import requests

img_url = 'https://img-home.csdnimg.cn/images/20201124032511.png'
img_data = requests.get(img_url).content
img = np.asarray(bytearray(img_data), dtype="uint8")
img = cv2.imdecode(img, cv2.IMREAD_COLOR)
print(img.shape)
print(img.size)
print(img.dtype)

Python获取网络图片扩展名、大小_计算机视觉_02

OpenCV + PIL

先用PIL读取网络图片,再转换为OpenCV格式。

import requests
from PIL import Image
url_img = 'https://img-home.csdnimg.cn/images/20201124032511.png'
response = requests.get(url_img, stream=True)
img = Image.open(response.raw)
img = cv.cvtColor(np.array(img), cv.COLOR_RGB2BGR)
print(img.shape)

Python获取网络图片扩展名、大小_opencv_03

参考

​​https://docs.opencv.org/4.x/d6/d00/tutorial_py_root.html​​​

https://pillow.readthedocs.io/en/stable/
https://xujinzh.github.io/2021/10/03/opencv-read-image-video-online/


举报

相关推荐

0 条评论