title: 【Python】Pillow图片处理库
type: categories
date: 2017-03-02 15:26:19
categories: Python
tags: [PIL, Pillow]
Pillow是Python中的图片处理库,来自于PIL,使用时 import PIL;
安装
Python2中安装命令:
sudo pip install PIL
Python3中安装命令:
sudo pip install Pillow
创建
mg = Image.open('pic.JPG')
print(img.size, img.mode) # (3264, 2448) RGB
缩略图
size = (200, 200)
img.thumbnail(size)
img.save('/Users/zhangzhao/PycharmProjects/pillow_demo/thum.JPEG')
print(img.size, img.mode) # (200, 150) RGB
剪切图
剪切函数 crop(box):box = (left, upper, right, lower)
注意:right > left; lower > upper;
剪切后图片长宽: width = right - left; height = lower - upper;
frame = (1632, 0, 3264, 1224) # 图片四分之一右上角部分
crop_img = Image.open('pic.JPG').crop(frame)
crop_img.save('/Users/zhangzhao/PycharmProjects/pillow_demo/crop.jpg')
#crop_img.show()
print(crop_img.size) # (1632, 1224)
几何转换
尺寸转换
resize_out = crop_img.resize((200, 200))
#resize_out.show()
角度转换
rotate_out = crop_img.rotate(45) # 逆时针 45
#rotate_out.show()
模式转换
change_mode = resize_out.convert('L')
print(resize_out.mode) # RGB
print(change_mode.mode) # L