scipy.misc包中的toimage和fromimage

对 Image 打开的图像做二值化以及黑白花处理时,找资料说要导包 from scipy.misc import fromimage, toimage

自己实验发现这包用不了了,找资料找到说 toimage 已经被 Image.fromarray替换;而 fromimage 没有资料。

解决办法:下边代码中提供了替换原来 fromimage与toimage 的方法

import numpy as np
from PIL import Image

# 打开图像,并灰度化
img = Image.open(img_path).convert('L')

# 1. 因为打开的图像是 Image 格式的,我们查看不了。所以将其转换为 数组格式,这步用 np 包实现。
img = np.asarray(img)  # 图像作为数组进行操作

# 2. 对转换好的数组图像进行操作
img = (np.ceil(img / 255.0) * 255.0).astype('uint8')  # 我这里是黑白化 

# 3. 最后将处理完之后的图像数组再次转化为 Image格式的图像,进行保存
img = Image.fromarray(img)

img.save()