十四:常用的第三方模块:
除了内建的模块外 Python还有大量的第三方模块 基本上 第三方模块都会在PyPI-the Python Package Index 上注册
PIL: python Imageing Library Python平台事实上的图像处理标准库 PLI功能非常的强大 但API却非常简单常用
操作图像:
#-*-coding:utf-8-*-
import Image
#打开一个JPG图像文件im=Image.open('E:/python/workspace/1.jpg')
#获得图像尺寸w,h=im.size#缩放50%
im.thumbnail((w//2,h//2))
#把缩放后的图像用jpeg格式保存im.save('E:/python/workspace/3.jpg','jpeg')
比如模糊图像 可以如下:
#-*-coding:utf-8-*-
import Image,ImageFilter
im=Image.open('E:/python/workspace/1.jpg')im2=im.filter(ImageFilter.BLUR)im2.save('E:/python/workspace/4.jpg','jpeg')
比如可以如下生成字母验证码:
#-*-coding:utf-8-*-
import Image,ImageDraw,ImageFont,ImageFilterimport random
#随机字母def rndChr():return chr(random.randint(65,90))
#随机颜色1def rndColor():return (random.randint(64,255),random.randint(64,255),random.randint(64,255))
#随机颜色2def rndColor2():return (random.randint(32,127),random.randint(32,127),random.randint(32,127))
#240*60
width=60*4height=60image=Image.new('RGB',(width,height),(255,255,255))#创建font对象font=ImageFont.truetype('E/:python/workspace/arial.ttf',36)#创建Draw 对象draw=ImageDraw.Draw(image)#填充每一个色素for x in range(width):for y in range(height):draw.point((x,y),fill=rndColor())
#输出文字for t in range(4):draw.text((60*t+10,10),rndChr,font=font,fill=rndColor2())#模糊image=image.filter(ImageFilter.BLUR)iamge.save('E:/python/workspace/5code.jpg','jpeg')