欢迎访问我的网站,希望内容对您有用,感兴趣的可以加入免费知识星球。

Python实用模块(六)pillow

实用模块 迷途小书童 4年前 (2019-12-03) 3979次浏览 0个评论

软硬件环境

  • ubuntu 18.04 64bit
  • anaconda with python 3.6
  • pillow

pillow

PILPython Image Library,是python图像处理的标准库,不过它仅仅支持python2,后来有人在原来版本的基础上增加了对python3的支持,就形成了Pillow这个库并且开源了,地址是https://github.com/python-pillow/Pillow

安装Pillow

pip install pillow

示例:

# -*- coding: utf-8 -*-
# @Time    : 18-11-13 下午2:22
# @Author  : xugaoxiang
# @Email   : xugx.ai@gmail.com
# @Website : https://xugaoxiang.com
# @File    : test.py.py
# @Software: PyCharm

from PIL import Image, ImageFilter

# 测试图片大小为400x400
img = Image.open('test.jpg')

# 图像的缩放
w, h = img.size
img.thumbnail((w/2, h/2))
img.save('thumbnail.jpg')

# 图像的模糊,使用的是ImageFilter,它可以做很多事情,如高斯模糊ImageFilter.GaussianBlur、普通模糊ImageFilter.BLUR、边缘增强ImageFilter.EDGE_ENHANCE等等
img_blur = img.filter(ImageFilter.BLUR)
img_blur.save('blur.jpg')

# 图像的裁剪
img_crop = img.crop((0, 0, 200, 200))
img_crop.save('crop.jpg')

# 图像的粘贴,原图像已经变化
img.paste(img_crop, (200, 200))
img.save('paste.jpg')

# 图像的旋转,原图像保持不变
img.rotate(90).save('rotate_90.jpg')
img.rotate(180).save('rotate_180.jpg')
img.rotate(270).save('rotate_270.jpg')
img.rotate(50).save('rotate_50.jpg')

thumbnail

blur

crop

paste

rotate

参考资料

  1. https://pillow.readthedocs.io/en/latest/handbook/index.html
喜欢 (0)

您必须 登录 才能发表评论!