------------恢复内容开始------------
1.1PIL:Python图像处理类库
读取一幅图像
1 from PIL import Image 2 pil_img = Image.open('imagepath') 3 pil_img.show()
如图:

颜色转换,比如转换为灰度图
1 from PIL import Image 2 pil_img = Image.open('imagepah').convert('L') 3 pil_img.show()

转换图像格式(save()方法)
1 from PIL import Image 2 import os 3 for infile in filelist: 4 outfile = os.path.splitext(infile)[0] + ".jpg" 5 if infile != outfile: 6 try: 7 Image.open(infile).save(outfile) 8 except IOError: 9 print "cannot convert", infile
创建略缩图 pil_img.thumbnail((128,128)) pil_img.show()

复制和粘贴图像区域
(复制)使用crop()方法裁剪指定区域(box中参数为坐标(左,上,右,下),左上角坐标为0,0):
box = (100,100,400,400) region = pil_img.crop(box) region.show()

把剪切下来的部分旋转180度,在粘贴到原来的位置
region = region.transpose(Image.ROTATE_180)
pil_img.paste(region,box)
pil_img.show()

调整尺寸和旋转
resize()方法参数为元组,规定图片的大小
out = pil_img.resize((128,128))
out.show()

旋转
out = pil_img.rotate(45)
out.show()

------------恢复内容结束------------
浙公网安备 33010602011771号