PIL 是 Python 最常用的图像处理库,在 Python 2.x 中是 PIL 模块,在 Python 3.x 中已经替换成 pillow 模块,安装 PIL :pip install pillow

  1. Python 查看图片的一些属性
#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PIL import Image

image = Image.open("dora.jpg")    # 打开一个图片对象
print(image.format)               # 查看图片的格式,结果为'JPEG'
print(image.size)                 # 查看图片的大小,结果为(800,450)分别表示宽和高的像素
print(image.mode)                 # 查看图片的模式,结果为'RGB',其他模式还有位图模式、灰度模式、双色调模式等等
  1. Python 调整图片大小
#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PIL import Image

image = Image.open("dora.jpg")    # 打开一个图片对象
out = image.resize((128, 128))    # 把图片调整为宽128像素,高128像素 
out.show()                        # 查看图片
  1. Python 旋转图片
#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PIL import Image

image = Image.open("dora.jpg")    # 打开一个图片对象
out = image.rotate(45)            # 将图片逆时针旋转45度
out.show()                        # 查看图片
  1. Python 翻转图片
#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PIL import Image

image = Image.open("dora.jpg")                  # 打开一个图片对象
out = image.transpose(Image.FLIP_LEFT_RIGHT)    # 左右翻转图片,如果要上下翻转参数为'Image.FLIP_TOP_BOTTOM'
out.show()                                      # 查看图片
  1. 上面几种对图片操作的功能基本上是使用的函数的方法不同所实现的图片效果不同。