1 验证基础知识
1.1 Python生成随机验证码,需要使用PIL模块.

  1. # 安装
  2. pip3 install pillow

1.2 创建图片

  1. from PIL import Image
  2. img = Image.new(mode="RGB", size=(120, 30), color=(125, 255, 255))
  3.  
  4. # 保存图片到本地
  5. with open("code.png",'wb') as f:
  6. img.save(f,format="png")

1.3 创建画笔

  1. from PIL import Image
  2. from PIL import ImageDraw
  3. img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
  4. draw = ImageDraw.Draw(img, mode="RGB")

1.4 画点

  1. from PIL import Image
  2. from PIL import ImageDraw
  3. img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
  4. draw = ImageDraw.Draw(img, mode="RGB")
  5. # 第一个参数:表示坐标
  6. # 第二个参数:表示颜色
  7. draw.point([100,20], fill="red")
  8. draw.point([50,10], fill=(255, 135, 255))
  9. with open("code.png",'wb') as f:
  10. img.save(f,format="png")

1.5 画线

  1. from PIL import Image
  2. from PIL import ImageDraw
  3. img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
  4. draw = ImageDraw.Draw(img, mode="RGB")
  5. # 第一个参数:表示起始坐标和结束坐标
  6. # 第二个参数:表示颜色
  7. draw.line((10, 10, 10, 30), fill="red")
  8. draw.line((10, 10, 30, 10), fill=(255, 135, 255))
  9. with open("code.png",'wb') as f:
  10. img.save(f,format="png")

1.6 画圆

  1. from PIL import Image
  2. from PIL import ImageDraw
  3. img = Image.new(mode="RGB", size=(150, 150), color=(255,255,255))
  4. draw = ImageDraw.Draw(img, mode="RGB")
  5. # 第一个参数:表示起始坐标和结束坐标(圆要画在中间)
  6. # 第二个参数:表示开始角度
  7. # 第三个参数:表示结束角度
  8. # 第四个参数:表示颜色
  9. draw.arc((50, 50, 100, 100), 0, 360, fill="red")
  10. with open("code.png",'wb') as f:
  11. img.save(f,format="png")

1.7 写文本

  1. from PIL import Image
  2. from PIL import ImageDraw
  3. img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
  4. draw = ImageDraw.Draw(img, mode="RGB")
  5. # 第一个参数:表示起始坐标
  6. # 第二个参数:表示写入内容
  7. # 第三个参数:表示颜色
  8. draw.text([20, 10], "python", fill="red")
  9. with open("code.png",'wb') as f:
  10. img.save(f,format="png")

1.8 设置特殊字体

  1. from PIL import Image, ImageDraw, ImageFont
  2. img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
  3. draw = ImageDraw.Draw(img, mode="RGB")
  4. # 第一个参数:表示字体路径
  5. # 第二个参数:表示字体大小
  6. font = ImageFont.truetype("kumo.ttf", 25)
  7. # 第一个参数:表示起始坐标
  8. # 第二个参数:表示写入内容
  9. # 第三个参数:表示颜色
  10. # 第四个参数:表示字体
  11. draw.text([30, 0], "python", fill="red", font=font)
  12. with open("code.png",'wb') as f:
  13. img.save(f,format="png")

2 应用

2.1 应用方法

  1. import random
  2. from io import BytesIO
  3. from PIL import Image, ImageDraw, ImageFont
  4.  
  5. def check_code(width=120, height=30, char_length=5, font_file='kumo.ttf', font_size=28):
  6. f = BytesIO()
  7. img = Image.new(mode='RGB', size=(width, height),
  8. color=(random.randint(50, 250), random.randint(50, 255), random.randint(50, 255)))
  9. draw = ImageDraw.Draw(img, mode='RGB')
  10.  
  11. char_list = []
  12. # 画字
  13. for i in range(char_length):
  14. char = random.choice([chr(random.randint(65, 90)), str(random.randint(1, 9)), chr(random.randint(97, 122)), ])
  15. font = ImageFont.truetype("blog/static/fonts/"+font_file, font_size)
  16. draw.text([i * 24, 0], char, (random.randint(200, 255), random.randint(0, 50), random.randint(0, 50)),
  17. font=font)
  18. char_list.append(char)
  19.  
  20. def rndColor():
  21. """
  22. 生成随机颜色
  23. :return:
  24. """
  25. return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))
  26.  
  27. # 写干扰点
  28. for i in range(40):
  29. draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
  30.  
  31. # 写干扰圆圈
  32. for i in range(40):
  33. draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
  34. x = random.randint(0, width)
  35. y = random.randint(0, height)
  36. draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())
  37.  
  38. # 画干扰线
  39. for i in range(5):
  40. x1 = random.randint(0, width)
  41. y1 = random.randint(0, height)
  42. x2 = random.randint(0, width)
  43. y2 = random.randint(0, height)
  44. draw.line((x1, y1, x2, y2), fill=rndColor())
  45.  
  46. img.save(f, "png")
  47. data = f.getvalue()
  48. s_code = ''.join(char_list)
  49. return data,s_code

2.2 调用方法

  1. def get_valid_img(request):
  2. '''
  3. 登录和注册的验证码
  4. :param request:
  5. :return:
  6. '''
  7. data,s_code = check_code()
  8. request.session["valid_code"] = s_code
  9. return HttpResponse(data)

2.3 效果展示

python实现图片验证码的更多相关文章

  1. python随机图片验证码的生成

    Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 from PIL import Im ...

  2. 字符识别Python实现 图片验证码识别

    字符型图片验证码识别完整过程及Python实现 1   摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...

  3. python 识别图片验证码报IOError

    说一下困扰了我一周的问题:识别图片验证码 本来我按照安装步骤(http://www.cnblogs.com/yeayee/p/4955506.html?utm_source=tuicool&u ...

  4. python添加图片验证码

    1.创建验证码模块 #!/usr/bin/env python # -*- coding:utf-8 -*- import random from PIL import Image, ImageDra ...

  5. python爬虫20 | 小帅b教你如何使用python识别图片验证码

    当你在爬取某些网站的时候 对于你的一些频繁请求 对方会阻碍你 常见的方式就是使用验证码 验证码的主要功能 就是区分你是人还是鬼(机器人) 人 想法设法的搞一些手段来对付技术 而 技术又能对付人们的想法 ...

  6. 【Python实现图片验证码】

    "```python import base64 import random from PIL import Image from PIL import ImageDraw # 画笔对象 f ...

  7. Python实现图片验证码识别

    转载地址:https://blog.csdn.net/EB_NUM/article/details/77060009 具体想要实现上面的代码需要安装两个包和一个引擎 在安装之前需要先安装好Python ...

  8. 字符型图片验证码识别完整过程及Python实现

    字符型图片验证码识别完整过程及Python实现 1   摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...

  9. 【python】带图片验证码的登录自动化实战

    近期在跟进新项目的时候,整体的业务线非常之长,会一直重复登录退出不同账号的这个流程,所以想从登录开始实现部分的自动化.因为是B/S的架构,所以采用的是selenium的框架来实现.大致实现步骤如下: ...

随机推荐

  1. phpmywind目录结构

    phpmywind目录结构了解 admin/ 后台管理目录 admin/editor/ 后台编辑器存放目录 admin/inc/ 后台公用文件引用目录 admin/plugin/ 后台插件存放目录 a ...

  2. RTM,RTW,GA等软件版本号详解

    一直以来,对于新手而言,软件的版本号都是个比较困扰人的问题,什么Beta.RC,再来个RTM.RTW....头大了吧?RTM和RTW有什么区别?借此机会,就给大家介绍一下这方面的小知识吧. 1.软件开 ...

  3. 【swupdate文档 三】SWUpdate: 嵌入式系统的软件升级

    SWUpdate: 嵌入式系统的软件升级 概述 本项目被认为有助于从存储媒体或网络更新嵌入式系统.但是,它应该主要作为一个框架来考虑,在这个框架中可以方便地向应用程序添加更多的协议或安装程序(在SWU ...

  4. Oracle-AWR报告简介及如何生成【转】

    AWR报告 awr报告是oracle 10g及以上版本提供的一种性能收集和分析工具,它能提供一个时间段内整个系统资源使用情况的报告,通过这个报告,我们就可以了解Oracle数据库的整个运行情况,比如硬 ...

  5. elasticsearch删除索引报错【原】

    如果elasticsearch删除索引报错 curl -X DELETE 'http://10.73.26.66:9200/httpd-34-2017.08.15' {"error" ...

  6. Linux运维常见问题解决集锦【转】

    作为linux运维,多多少少会碰见这样那样的问题或故障,用点心,平时多注意积累,水平肯定越来越高. 下面就是常见问题解决集锦:   1.shell脚本不执行 问题:某天研发某同事找我说帮他看看他写的s ...

  7. 剑指offer算法题

    数组中只出现一次的数字(一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字): 解法在于位运算中的异或,直接异或可以得到这两个数的异或,按照最后的有效数字位可以 ...

  8. ubuntu上安装MySQL详解

     1. 安装 在终端输入 sudo apt-get install mysql-server mysql-client 回车 2.安装完成后检测MySQL的状态 systemctl status my ...

  9. 出现ERROR: While executing gem ... (Gem::FilePermissionError)这种错误的解决办法

    重新安装ruby即可解决 brew install ruby

  10. csu 1549: Navigition Problem(几何,模拟)

    1549: Navigition Problem Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 305  Solved: 90[Submit][Stat ...