日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python生成驗證碼實例

系統 1941 0

本文實例展示了Python生成驗證碼的方法,具有很好的實用價值。分享給大家供大家參考。具體實現方法如下:

前臺頁面代碼如下:

后臺程序如下:

            
import StringIO
import Image, ImageDraw, ImageFont, random  #相應的模塊需要安裝
from xxx.settings import authcode_font #請確保改字體存在

def make_image(request):
  mp = hashlib.md5()
  mp.update(str(datetime.datetime.now())+str(random.random()))  
  mp_src = mp.hexdigest()
  rand_str = mp_src[0:6]
  font = ImageFont.truetype(authcode_font, 25)
  width = 75
  height = 30
  im = Image.new('RGB',(width,height),'#%s'%mp_src[-7:-1])
  draw = ImageDraw.Draw(im)
  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))
  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))
  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))
  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))
  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))
  draw.text((5,2), rand_str, font=font)  
  del draw  
  buffer = StringIO.StringIO()
  im.save(buffer,'jpeg')
  httpResponse = HttpResponse(content=buffer.getvalue(),mimetype="image/jpeg")
  request.session['auth_code'] = rand_str
  return httpResponse


          

程序效果如下:


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 深圳市| 贺兰县| 赣榆县| 延寿县| 黔西| 元阳县| 朝阳区| 金川县| 陆良县| 噶尔县| 长兴县| 荆门市| 泊头市| 云南省| 牙克石市| 伊通| 政和县| 桂平市| 曲沃县| 厦门市| 蓝山县| 河东区| 钟祥市| 苍溪县| 乐亭县| 上林县| 金堂县| 曲靖市| 增城市| 金华市| 兰西县| 拜泉县| 临江市| 木兰县| 新巴尔虎左旗| 丰顺县| 绍兴县| 太保市| 银川市| 连江县| 嘉峪关市|