Python 处理图片,,PIL 是 Pyth


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 Imageimage = Image.open("dora.jpg")    # 打开一个图片对象print(image.format)               # 查看图片的格式,结果为‘JPEG‘print(image.size)                 # 查看图片的大小,结果为(800,450)分别表示宽和高的像素print(image.mode)                 # 查看图片的模式,结果为‘RGB‘,其他模式还有位图模式、灰度模式、双色调模式等等

2、Python 调整图片大小

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

效果图:

技术分享图片

3、Python 旋转图片

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

效果图:

技术分享图片

4、Python 翻转图片

#!/usr/bin/env python#-*- coding:utf-8 -*-from PIL import Imageimage = Image.open("dora.jpg")                  # 打开一个图片对象out = image.transpose(Image.FLIP_LEFT_RIGHT)    # 左右翻转图片,如果要上下翻转参数为‘Image.FLIP_TOP_BOTTOM‘out.show()                                      # 查看图片

效果图:

技术分享图片

Python 处理图片

评论关闭