详解python读取image

yipeiwu_com5年前Python基础

python 读取image

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

安装库

pip install matplotlib pillow scipy

用法

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

skimage

安装 pip install -U scikit-image

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

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

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

以上所述是小编给大家介绍的python读取image详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

Python从零开始创建区块链

Python从零开始创建区块链

作者认为最快的学习区块链的方式是自己创建一个,本文就跟随作者用Python来创建一个区块链。 对数字货币的崛起感到新奇的我们,并且想知道其背后的技术——区块链是怎样实现的。 但是完全搞懂...

Python中datetime模块参考手册

前言 Python提供了多个内置模块用于操作日期时间,像 calendar,time,datetime。time模块提供的接口与C标准库 time.h 基本一致。相比于 time 模块,...

老生常谈python之鸭子类和多态

一、 什么是多态 <1>一种类型具有多种类型的能力 <2>允许不同的对象对同一消息做出灵活的反应 <3>以一种通用的方式对待个使用的对象 <4&...

Python内置数据结构与操作符的练习题集锦

第一题: give you two var a and b, print the value of a+b, just do it! 根据提议,给出两个变量 a 和 b 并打印出 a+b...

Python实现代码统计工具(终极篇)

Python实现代码统计工具(终极篇)

本文对于先前系列文章中实现的C/Python代码统计工具(CPLineCounter),通过C扩展接口重写核心算法加以优化,并与网上常见的统计工具做对比。实测表明,CPLineCount...