生成随机图片验证码

#!/usr/bin/env python3 

#-*- coding:utf-8 -*- 

 

#常用第三方模块

#PIL: Python Imaging Library,已经是Python平台事实上的图像处理标准库了。PIL功能非常强大,但API却非常简单易用。

#由于PIL仅支持到Python2.7,加上年久失修,于是一群志愿者在PIL的基础上创建了兼容版本Pillow,支持最新的Python 3.X ,又加入了许多新特性,因此,

#我们可以直接安装使用Pillow.生成随机验证码:

from PIL import Image,ImageDraw,ImageFont,ImageFilter

import random 

#随机字母

def rndChar():

    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))

 

#创建pont对象:

font=ImageFont.truetype('e:/work/chinese.TTF',36)

#创建Drev对象:

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')

#我们用随机颜色填充背景,再画上文字,最后对图像进行模糊,得到验证码图片。

 

转载于:https://www.cnblogs.com/Ting-light/p/9548174.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
随机生成6位图片验证码. /// <summary> /// PicHandler1 的摘要说明 /// </summary> public class PicHandler1 : IHttpHandler, IRequiresSessionState { private string mCheckNo = string.Empty; protected ImgBuilder _ImgBuilder = new ImgBuilder(); protected VryImgGen _ImgBuilderNew = new VryImgGen(); private string _text = string.Empty; private string _font = "宋体"; private int _fontSize = 8; private int _padding = 2; public void ProcessRequest(HttpContext context) { mCheckNo = DisCheckNo(); context.Session["CheckCode"] = mCheckNo; this._ImgBuilder.FontSize = this._fontSize; this._ImgBuilder.Padding = this._padding; if (!string.IsNullOrEmpty(this._font)) { this._ImgBuilder.Fonts = new string[] { this._font }; } this._ImgBuilderNew.ChaosWight = 40; this._ImgBuilderNew.FontSize = 25; this._ImgBuilderNew.Padding = 3; System.Drawing.Bitmap image = this._ImgBuilderNew.CreateImage(mCheckNo); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); context.Response.ClearContent(); context.Response.ContentType = "image/Jpeg"; context.Response.BinaryWrite(ms.ToArray()); context.Session["CheckCode"] = mCheckNo.ToString(); //如果没有实现IRequiresSessionState,则这里会出错,也无法生成图片 context.Response.End(); } //验证码生成 protected string DisCheckNo() { string hash = HashCode.GetNext(); string CheckNo = string.Empty; Random rd = new Random(DateTime.Now.Millisecond); for (int i = 0; i < 6; i++) { CheckNo += hash.Substring(rd.Next(1, hash.Length - 1), 1); } CheckNo = CheckNo.Replace("0", rd.Next(1, 9).ToString()); CheckNo = CheckNo.Replace("o", rd.Next(1, 9).ToString());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值