PIL:Python Image Library
1. Image.fromarray ⇔ ⇔ ⇔ ⇔ ⇔ \Leftrightarrow ⇔ ⇔ ⇔⇔ np.asarray
def read_image(path): img = Image.open(path) img = img.convert('L') return np.asarray(img, dtype='float64')/255.def save_image(array, path): array[array > 255] = 255 array[array < 0] = 0 array.convert('RGB').save(path)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
2. 重要模块及其成员函数
Image.fromarray()
顾名思义,将二维数组转换为图像。
from PIL import Imageimport numpy as nparr = (np.eye(200)*255).astype('uint8')im = Image.fromarray(arr)imrgb = Image.merge('RGB', (im, im, im))imrgb.show()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
 PIL 读取获得的图像矩阵与 numpy 下的多维数组
import numpy as npfrom PIL import Imageimg = Image.open(open('./images/3wolfmoon.jpg')) # Image.open 接受一个文件句柄img = np.asarray(img, dtype='float64')/255.img.shape # (639, 516, 3) # 做到这一步还不够,如果是彩色图像 # img.shape = (height, width, ndim) # 并不是 numpy 中所习惯的维度配置img = img.transpose(2, 0, 1)img.shape # (3, 639, 516)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow