Python第三方库使用 —— PIL
2015-12-10 10:43
796 查看
因为语法的简洁,造就了功能的强大,因为开源,全世界的顶尖程序员们(各个行业的)辛勤地无私地也较为容易地为python贡献各式各样的第三方的库,再因为安装的便捷,普及也较为容易。如此种种,造就了python纷繁的世界。
PIL:
PIL:
Python Image Library
Image.fromarray ⇔ np.asarray
def read_image(path): img = Image.open(path) img = img.convert('L') return np.asarray(img, dtype='float64')/256. def save_image(array, path): array[array > 255] = 255 array[array < 0] = 0 array.convert('RGB').save(path)
重要模块及其成员函数
Image.fromarray()
顾名思义,将二维数组转换为图像。from PIL import Image import numpy as np arr = (np.eye(200)*255).astype('uint8') im = Image.fromarray(arr) imrgb = Image.merge('RGB', (im, im, im)) imrgb.show()
PIL 读取获得的图像矩阵与 numpy 下的多维数组
import numpy as np from PIL import Image img = Image.open(open('./images/3wolfmoon.jpg')) # Image.open 接受一个文件句柄 img = np.asarray(img, dtype='float64')/256. img.shape # (639, 516, 3) # 做到这一步还不够,如果是彩色图像 # img.shape = (height, width, ndim) # 并不是 numpy 中所习惯的维度配置 img = img.transpose(2, 0, 1) img.shape # (3, 639, 516)
相关文章推荐
- Python与rrdtool的结合模块
- 为什么选择python
- SyntaxError: Non-ASCII character ‘\xe5′ in file和在代码中插入中文,python中文注释
- 自学Python爬虫遇到的一些问题
- 欢迎使用CSDN-markdown编辑器
- python操作文件常用写法
- python with语句理解
- Python_变量的作用域
- PyCharm_汉化包_编译器字体和背景怎么设置?_怎么添加多个python解释器?
- python 函数 字符串读入问题
- python 操作mysql数据库
- python使用SQL语句操作数据库中文乱码的解决
- WxPython学习
- python 自省1
- Python读取UTF-8编码文件并使用命令行执行时输出结果的问题
- 读完了简明Python教程(a bite of Python)
- Python第三方库使用——splinter
- python脚本编程:批量复制或删除文件
- python:类
- Python——迭代器和解析(3)