脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 详解python读取image

详解python读取image

2021-06-11 00:41ke1th Python

这篇文章主要介绍了python读取image的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

python 读取image

在python中我们有两个库可以处理图像文件,scipy和matplotlib.

安装库

?
1
pip install matplotlib pillow scipy

用法

?
1
2
3
from scipy.misc import imread
data = imread(image_root)
#data是 ndarray对象
?
1
2
3
import matplotlib.image as mpimg
data = mpimg.imread(image_root)
#data是 ndarray对象

skimage

安装 pip install -U scikit-image

?
1
2
3
4
5
from skimage.io import imread
img = imread(file_path) # 返回的是 ndarray
# 这里需要注意的是
# imread 读取 8-bit png 的时候莫名奇妙的读出个 3-channel 的图片
# from scipy.misc import imread 这个 imread 也是一个尿性

PIL

安装 pip install pillow

?
1
2
3
4
5
6
7
from PIL import Image
import numpy as np
img_obj = Image.open(file_path)
img_array = np.array(img_obj, dtype=np.uint8)
 
# 无论是 jpg 还是 png 都能正确读取
\

matplotlib

安装 pip install matplotlib

?
1
2
3
4
from matplotlib.image import imread
img = imread(img_path) # 返回 ndarray
# 这个imread 读 png 的时候,返回ndarray 的类型是 uint8
# 读 png 的时候,返回 ndarray 是 float32, 8-bit png 也能读出 3-channel,活在梦里

以上所述是小编给大家介绍的python读取image详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/u012436149/article/details/53738865

延伸 · 阅读

精彩推荐