转载:http://www.cnblogs.com/way_testlife/archive/2011/04/20/2022997.html
本文是节选自 PIL handbook online 并做了一些简单的翻译
只能保证自己看懂,不保证翻译质量。欢迎各位给出意见。
------------------------------------------------------
Image 模块提供了一个同名类(Image),也提供了一些工厂函数,包括从文件中载入图片和创建新图片。例如,以下的脚本先载入一幅图片,将它旋转 45 度角,并显示出来:
1 >>>from PIL import Image
2 >>>im = Image.open("j.jpg")
3 >>>im.rotate(45).show()
2 >>>im = Image.open("j.jpg")
3 >>>im.rotate(45).show()
下面这个脚本则创建了当前目录下所有以 .jpg 结尾的图片的缩略图。
1 from PIL import Image
2 import glob, os
3
4 size = 128, 128
5 for infile in glob.glob("*.jpg"):
6 file, ext = os.path.splitext(infile)
7 im = Image.open(infile)
8 im.thumbnail(size, Image.ANTIALIAS)
9 im.save(file + ".thumbnail", "JPEG")
2 import glob, os
3
4 size = 128, 128
5 for infile in glob.glob("*.jpg"):
6 file, ext = os.path.splitext(infile)
7 im = Image.open(infile)
8 im.thumbnail(size, Image.ANTIALIAS)
9 im.save(file + ".thumbnail", "JPEG")
Image 类中的函数。
0. new : 这个函数创建一幅给定模式(mode)和尺寸(size)的图片。如果省略 color 参数,则创建的图片被黑色填充满,如果 color 参数是 None 值,则图片还没初始化。
1 Image.new( mode, size ) => image
2 Image.new( mode, size, color ) => image
2 Image.new( mode, size, color ) => image