栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Python

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

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)

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)

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)

参考

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/

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/1071411.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号