您的位置:首页 > 编程语言 > Python开发

【Python】Pillow图片处理库

2017-03-02 15:45 239 查看
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


http://www.cnblogs.com/wbin91/p/3971079.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: