首页 > 代码库 > Python之Python Imaging Library

Python之Python Imaging Library

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到原先的图片对象中。  


  

 

Python之Python Imaging Library