Python 图像处理的库 Pillow,opencv 和 scikit-image 的基本用法

Python 图像处理的库 Pillow,opencv 和 scikit-image 的基本用法

    • Pillow 包的使用
      • Pillow 基本用法
      • 生成文字图片
      • Image 图像与 base64 字符串互相转换
      • Image 图像与 Numpy 数组相互转换
    • scikit-image 的使用
    • Open-CV
    • 参考:

基于python脚本语言开发的数字图片处理包有 PIL,Pillow,opencv,scikit-image等,其中:

  • PIL和Pillow只提供最基础的数字图像处理,功能有限。PIL只支持 Python2, Pillow是PIL的一个派生分支,支持 Python3.
  • opencv 实际上是一个c++库,只是提供了python接口,更新速度非常慢。
  • scikit-image 是基于scipy的一款图像处理包,它将图片作为 numpy 数组进行处理,与matlab一样。

Pillow 包的使用

安装:
pip install Pillow

  • 官网:https://python-pillow.org/
  • Pillow的Github主页:https://github.com/python-pillow/Pillow
  • 文档:https://pillow.readthedocs.io/en/latest/index.html

Pillow 基本用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from PIL import Image
from PIL import ImageGrab

# Image.open 打开图片
img = Image.open('picture.png')  

# 获取剪贴板中复制的图片
img = ImageGrab.grabclipboard()

# 显示图像
img.show()

# 旋转图像
new_img = img.rotate(90)  # 逆时针旋转 90 度

# 图像的镜面翻转
new_img = img.transpose(Image.FLIP_LEFT_RIGHT)  # 水平翻转
new_img = img.transpose(Image.FLIP_TOP_BOTTOM)  # 垂直翻转

# 重新设置图像大小
new_img = img.resize((1280, 1964),Image.BILINEAR)

# 保存图片
new_img.save('new_picture.png')

生成文字图片

1
2
3
4
5
6
from PIL import Image, ImageFont, ImageDraw

img = Image.new("RGB", (32, 32), "black") # 新建图像,黑色 32*32
draw = ImageDraw.Draw(img)  # 可对img进行绘制
font = ImageFont.truetype(font_path, int(width * 0.9),) # 创建ImageFont对象
draw.text((0, 0), '陈', (255, 255, 255),font=font) # 在img中绘制文字

Image 图像与 base64 字符串互相转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import re
import base64
from io import BytesIO
from PIL import Image

# Image 图像转为二进制和 base64 字符串
img = Image.open('picture.png')  
buffer = BytesIO()
img.save(buffer, format='PNG')
byte_data = buffer.getvalue()   # 二进制编码,# bytes类型
base64_byte = base64.b64encode(byte_data)       # base64 编码,bytes类型
base64_str = base64_byte.decode()  # base64 编码,字符串类型

# base64 字符串转为 Image 图像
new_byte_data = base64.b64decode(base64_str)
img_data = BytesIO(new_byte_data)
new_img = Image.open(img_data)
new_img.show()

Image 图像与 Numpy 数组相互转换

1
2
3
4
5
6
7
8
9
10
from PIL import Image
import numpy as np

img = Image.open('picture.png')  

# Image 图像转为 numpy 数组
imarr = np.array(img)

# 将 numpy 数组转为 Image 对象
new_img = Image.fromarray(imarr * 2)

scikit-image 的使用

scikit-image 将图像读取为 Numpy 数组,操作也都是基于对数组的操作。

  • scikit-image 安装: pip install scikit-image
  • 官网:https://scikit-image.org/
  • 文档:https://scikit-image.org/docs/stable/
  • 示例库:https://scikit-image.org/docs/stable/auto_examples/
子模块 功能描述
color 颜色变换的模块
data 提供一些测试图片和数据
draw 图像绘制,包括线条、矩形、圆和文本等
exposure 图片强度调整,如亮度调整、直方图均衡等
feature 特征检测与提取等
filters 图像增强、边缘检测、排序滤波器、自动阈值等
io 读取、保存和显示图片或视频
measure 图像属性的测量,如相似性或等高线等
metrics 评估图像误差、相似性等
morphology 形态学操作,如开闭运算、骨架提取等
restoration 图像恢复
segmentation 图像分割
transform 几何变换或其它变换,如旋转、拉伸和拉东变换等
util 通用函数
viewer 其他查看图像的方法

基本用法

1
2
3
4
5
6
7
8
9
10
11
12
from skimage import io

# 打开图像
image = io.imread('picture.png')

type(image)  # numpy.ndarray

# 查看图像
io.imshow(image)

# 保存图片
io.imsave('new_picture.png', image)

Open-CV

安装:pip install opencv-python

pypi 网址:https://pypi.org/project/opencv-python/

由于外网的官方库下载太慢,可以使用豆瓣的源下载安装:pip install opencv-python -i https://pypi.douban.com/simple

OpenCV python 文档:https://docs.opencv.org/master/d6/d00/tutorial_py_root.html

基本用法:

1
2
3
4
5
6
7
8
9
10
11
import cv2

# 读取图片
im = cv2.imread(picfile)    
type(im)   # numpy.ndarray

# 显示图片
cv2.imshow('show image', im)

# 保存图片
cv2.imwrite('new_picture.png', im)

参考:

  • Python用Pillow(PIL)进行简单的图像操作
  • PIL.Image与Base64 String的互相转换
  • scikit-image 的基本用法: https://www.jianshu.com/p/f2e88197e81d
  • 图像的形态处理,OpenCV-Python教程(4、形态学处理)
  • opencv python 读取图像/显示图像/保存图像