马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 gzj137070928 于 2020-12-1 11:52 编辑
# 缩略图:thumbnail函数:thumbnail(size, Image.ANTIALIAS)
# 参数size为一个元祖,指定生成缩略图的大小
# 对原图修改后需要保存,处理后的图片不会被拉伸
# 实例:PIL生成验证码
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from PIL import ImageFilter
import random as r
def rndChar():
"""随机字母"""
return chr(r.randint(65,90))
def rndColor():
'''随机颜色1:文字'''
return (r.randint(64,255),r.randint(64,255),\
r.randint(64,255))
def rndColor2():
'''随机颜色2:背景'''
return (r.randint(32,127),r.randint(32,127),\
r.randint(32,127))
width = 60 * 4
height = 60
image = Image.new('RGB', (width, height),(255,255,255))
font = ImageFont.truetype('arial.ttf', 36)
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), rndChar(), font=font, fill=rndColor2())
# 模糊处理:
image = image.filter(ImageFilter.BLUR)
image.save('code.jpg', 'jpeg')
|