1 环境
图片处理的库:
- base64
- OpenCV
- Pillow
- matplotlib
2 不同库处理图片
2.1 base64
base64是Python自带的处理图像进行编解码的库。
import base64
image = "base64编码的图像"
with open("./path/image.png", "wb") as fdecode:
'''图像解码'''
image = base64.b64decode(image)
'''图像保存'''
fdecode.write(image)
2.2 OpenCV
OpenCV开源图像处理库。
import cv2
# 打开图片:图像编码为(h, w, channels)=(y, x, channels)
image_raw = cv2.imread(*.jpg)
# 显示图片
cv2.imshow(image_raw)
# 保存图片
cv2.imwrite(path, image_raw)
2.3 Pillow
import PIL
# 新建图层:通道,尺寸,背景
image_new = PIL.Image.new('RGB', (250, 250), 'black')
# 打开图片:图像编码为(h, w, channels)=(y, x, channels)
image_raw = PIL.Image.open(*.jpg)
# 读取图片raw
pil_image = PIL.Image.fromarray(image_raw)
# 显示图片
pil_image.show()
# 保存图片
image_raw.save()
2.4 matplotlit
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
# 新建图层
plt.figure()
# 读取图片:服务器只支持png格式,本地测试不限格式
# 图像编码为(h, w, channels)=(y, x, channels)
image_raw = mpimg.imread(*.png)
# 显示图片:imshow显示读取的文件
# show显示绘制的图片
plt.imshow(image_raw)
# 保存图片:服务器只支持png格式,本地保存不限格式
plt.savefig(*.png)
3 总结
序号 | 库源 | 读 | 显 | 存 |
---|---|---|---|---|
1 | base64 | read() | / | write() |
2 | OpenCV | cv2.imread() | cv2.imshow() | cv2.imwrite() |
3 | Pillow | PIL.Image.open() | PIL.Image.fromarray().show | PIL.Image.fromarray().save() |
4 | matplotlib | matplotlib.image.imread() | matplotlib.pyplot.imshow() | matplotlib.pyplot.savefig() |
基础博客:
(一)OpenCV之读写显示函数
(二)OpenCV绘图
python之matplotlib绘图
[参考文献]
[1]https://pillow.readthedocs.io/en/latest/handbook/tutorial.html
[2]https://matplotlib.org/users/image_tutorial.html
[3]https://docs.opencv.org/master/d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56