连接 NumPy 与 剩余世界
来源:互联网 发布:js数组的方法 编辑:程序博客网 时间:2024/05/17 14:26
连接 NumPy 与 剩余世界
# 来源:NumPy Cookbook 2e Ch4
使用缓冲区协议
# 协议在 Python 中相当于接口# 是一种约束import numpy as np import Image # from PIL import Image (Python 3) import scipy.misclena = scipy.misc.lena() # Lena 是 512x512 的灰度图像# 创建与 Lena 宽高相同的 RGBA 图像,全黑色data = np.zeros((lena.shape[0], lena.shape[1], 4), dtype=np.int8) # 将 data 的不透明度设置为 Lena 的灰度data[:,:,3] = lena.copy() # 将 data 转成 RGBA 的图像格式,并保存img = Image.frombuffer("RGBA", lena.shape, data, 'raw', "RGBA", 0, 1) img.save('lena_frombuffer.png')# 每个像素都设为 #FC0000FF (红色)data[:,:,3] = 255 data[:,:,0] = 222 img.save('lena_modified.png')
数组协议
from __future__ import print_function import numpy as np import Image import scipy.misc# 获取上一节的第一个图像lena = scipy.misc.lena() data = np.zeros((lena.shape[0], lena.shape[1], 4), dtype=np.int8) data[:,:,3] = lena.copy() img = Image.frombuffer("RGBA", lena.shape, data, 'raw', "RGBA", 0, 1) # 获取数组接口(协议),实际上它是个字典array_interface = img.__array_interface__ print("Keys", array_interface.keys())print("Shape", array_interface['shape']) print("Typestr", array_interface['typestr'])'''Keys ['shape', 'data', 'typestr'] Shape (512, 512, 4) Typestr |u1 '''# 将图像由 PIL.Image 类型转换回 np.arraynumpy_array = np.asarray(img) print("Shape", numpy_array.shape) print("Data type", numpy_array.dtype)'''Shape (512, 512, 4) Data type uint8'''
与 Matlab 和 Octave 交换数据
# 创建 0 ~ 6 的数组a = np.arange(7) # 将 a 作为 array 保存在 a.mat 中scipy.io.savemat("a.mat", {"array": a})'''octave-3.4.0:2> load a.mat octave-3.4.0:3> array array = 0 1 ... 6'''# 还可以再读取进来mat = io.loadmat("a.mat")print mat# {'array': array([[0, 1, 2, 3, 4, 5, 6]]), '__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file Platform: nt, Created on: Sun Jun 11 18:48:29 2017', '__globals__': []}
阅读全文
0 0
- 连接 NumPy 与 剩余世界
- 剩余类与剩余系
- numpy.ravel() 与numpy.flatten()
- numpy.flatten() 与 numpy.ravel()的区别
- numpy 数组与类型
- NumPy与ndarray简介
- python numpy与matplotlib
- Numpy与Pandas排序
- NumPy 复制与视图
- Numpy与Pandas
- Numpy与统计学
- numpy ndarray 与 array
- numpy与scipy安装
- Numpy-复制与视图
- numpy 维度与轴
- poj 3471 整除与剩余
- 中国剩余定理与扩展
- 4个入口8条光缆!我们这样与世界连接
- Win7系统怎么截图?几种简单的win7截图方法
- 286. Walls and Gates
- python装饰器使用
- JAVA集合-List集合
- 打开第三方应用
- 连接 NumPy 与 剩余世界
- Python3系的安装教程
- android 获取手机联系人
- 开始
- 新路程------imx6动态修改fb参数
- python爬虫学习笔记
- sublime使用指南
- JAVA集合-集合的遍历
- 目标检测领域