python学习笔记 常用第三方模块

来源:互联网 发布:管易erp软件 编辑:程序博客网 时间:2024/05/13 12:44
十四:常用的第三方模块
                    除了内建的模块外 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,ImageFilter
import random

#随机字母
def rndChr():
 return chr(random.randint(65,90))

#随机颜色1
def rndColor():
 return (random.randint(64,255),random.randint(64,255),random.randint(64,255))

#随机颜色2
def rndColor2():
 return (random.randint(32,127),random.randint(32,127),random.randint(32,127))

#240*60

width=60*4
height=60
image=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')    

0 0
原创粉丝点击