PIL(Python Imaging Library Python,图像处理类库)提供了通用的图像处理功能, 以及大量有用的基本图像操作,比如图像缩放、裁剪、旋转、颜色转换等。PIL 是免费的,可以从 http://www.pythonware.com/products/pil/ 下载。
利用 PIL 中的函数,我们可以从大多数图像格式的文件中读取数据,然后写入最常 见的图像格式文件中。PIL 中最重要的模块为 Image。
整理了一些可能会常用到的功能,直接上代码,请根据注释来学习:(如果你需要相关功能,自行注释掉无用的功能或copy出你想要的功能即可!)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 29 23:07:18 2018
@author: YancyLin
"""
from PIL import Image
import os
from pylab import *
# 读取图像
im = Image.open('1.jpg')
#转为灰度图
im = Image.open('empire.jpg').convert('L')
# 绘制图像
imshow(im)
#使用 crop() 方法可以从一幅图像中裁剪(复制出)指定区域
box = (100,100,400,400)
region = pil_im.crop(box)
#旋转
region = region.t