Pillow和Numpy的图像基本操作

来源:互联网 发布:excel数据统计分析方法 编辑:程序博客网 时间:2024/05/23 12:00

1.图像的加载、显示和保存

  Pillow与PIL模块不能同时安装,但是在import的时候仍然要用PIL来代替Pillow。Pillow中最重要的一个模块就是Image。

# !/usr/bin/python# -*- coding:utf-8 -*-from PIL import Image# 加载图像(路径必须双\\)pil_img = Image.open('D:\\1.tif')# 显示pil_img.show()# 另存为outfile = "2"+".jpg"pil_img.save(outfile)

2.图像的数组操作

  图像用Image.open的形式加载进来后是一个PIL的图像对象,为了方便用Numpy进一步操作,需要用array()转化为数组。

# !/usr/bin/python# -*- coding:utf-8 -*-from PIL import Imagefrom numpy import *# 加载图像(路径必须双\\)pil_img = Image.open('D:\\1.tif')# 转化为数组img = array(pil_img)# 获取图像参数height,width = img.shape[0:2]# 访问像素value = img[i,j,k]  #[行,列,channel]

采用数组处理完以后再变回图像:

pil_im2 = Image.fromarray(uint8(img))
1 0