基础使用
简介
PIL(Python Imaging Library)是Python一个强大方便的图像处理库,名气也比较大。
PIL官方网站:http://www.pythonware.com/products/pil/
Pillow是PIL的一个派生分支,但如今已经发展成为比PIL本身更具活力的图像处理库。目前最新版本是3.0.0。 Pillow的Github主页:https://github.com/python-pillow/Pillow
Pillow的文档(对应版本v3.0.0):https://pillow.readthedocs.org/en/latest/handbook/index.html Pillow的文档中文翻译(对应版本v2.4.0):http://pillow-cn.readthedocs.org/en/latest/ Pillow是PIL的一个派生分支,但如今已经发展成为比PIL本身更具活力的图像处理库。目前最新版本是3.0.0。
Pillow的Github主页:https://github.com/python-pillow/Pillow
基础概念
过滤器
安装
- 3.x
pip install pillow
python -m pip install pillow
./python3 -m pip install pillow -i https://pypi.tuna.tsinghua.edu.cn/simple
pip 安装
python -m pip
./python.exe -m pip install pillow -i https://pypi.tuna.tsinghua.edu.cn/simple/
引入
import PIL
# or
from PIL import Image
基础使用
from PIL import Image
模板示例
保存图片
im.save(fp, format, quality=75)
矩阵 倾斜
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import PERSPECTIVE [as 别名]
def draw_picture(img_src):
fap_path = os.path.join(os.path.dirname(__file__), "fap.png")
# ?????????? ????????
response = requests.get(img_src)
response.raise_for_status()
image_bytes = io.BytesIO(response.content)
# ?????????
image = Image.open(image_bytes)
fap_pic = Image.open(fap_path)
if image.mode != 'RGBA':
image = image.convert('RGBA')
image_width, image_height = image.size
fap_width, fap_height = fap_pic.size
def find_coeffs(pa, pb):
""" https://stackoverflow.com/questions/14177744/
????? ????????? ????, ??? ??????? - ???????
"""
matrix = []
for p1, p2 in zip(pa, pb):
matrix.append([p1[0], p1[1], 1, 0, 0, 0, -p2[0]*p1[0], -p2[0]*p1[1]])
matrix.append([0, 0, 0, p1[0], p1[1], 1, -p2[1]*p1[0], -p2[1]*p1[1]])
A = numpy.matrix(matrix, dtype=numpy.float)
B = numpy.array(pb).reshape(8)
res = numpy.dot(numpy.linalg.inv(A.T * A) * A.T, B)
return numpy.array(res).reshape(8)
trans_coeff = find_coeffs(
[(217,111),(412,115),(222,372),(403,371)],
[(0,0), (image_width-1,0), (0,image_height-1), (image_width-1, image_height-1)])
resp_pic = image.transform(fap_pic.size, Image.PERSPECTIVE, trans_coeff, Image.BILINEAR)
# ??????????? gesture ? image
resp_bytes = io.BytesIO()
Image.alpha_composite(resp_pic, fap_pic).save(resp_bytes, format='PNG')
resp_bytes.seek(0)
return resp_bytes