Python之Python Imaging Library

document:http://effbot.org/imagingbook/pil-index.htm http://pillow.readthedocs.io/en/3.1.x/index.html

from PIL import Image

1.打开图片

img = Image.open(fileName)

 

2.保存图片

img.save(imgName)

 

3.调整图片大小

def resize(self, width, height):
    o_width, o_height = img.size
    o_ratio = o_width/float(o_height)
    n_ratio = width/float(height)

    if o_ratio > n_ratio:
        re_ration = width/float(o_width)
        a_height = int(re_ration*o_height)
        img = self.img.resize((width,a_height),Image.ANTIALIAS)
    else:
        re_ration = height/float(o_height)
        a_width = int(re_ration*o_width)
        img = img.resize( (a_width,height),Image.ANTIALIAS)

    img.save('test.jpg')

 

4.新建图片

img = Image.new("RGB", (width, height), "black")

 

5.粘贴拷

box = (100,100,500,500)#设置要拷贝的区域  

region = im.crop(box)  

region = region.transpose(Image.ROTATE_180)#从字面上就可以看出,先把region中的Image反转180度,然后再放回到region中。  

im.paste(region, box)#粘贴box大小的region到原先的图片对象中。  


  

 

posted @ 2016-10-13 11:13  Shiyu_Huang  阅读(605)  评论(0编辑  收藏  举报