当前位置: 首页 > news >正文

python本地生成验证码图片

from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
from captcha.image import ImageCaptcha
import random, stringdef get_captcha_picture():chr_all = string.ascii_uppercase + string.digits  # 只使用大写字母和数字,避免混淆chr_4 = ''.join(random.sample(chr_all, 4))# 使用PIL创建验证码图片(替代ImageCaptcha)width, height = 120, 40image = Image.new('RGB', (width, height), color=(240, 240, 240))draw = ImageDraw.Draw(image)try:# 尝试使用系统字体font = ImageFont.truetype("arial.ttf", size=24)except IOError:try:# 尝试其他常见字体font = ImageFont.truetype("DejaVuSans.ttf", size=24)except IOError:# 使用默认字体font = ImageFont.load_default()# 绘制验证码文本for i, char in enumerate(chr_4):x = 10 + i * 25 + random.randint(-3, 3)y = 5 + random.randint(-3, 3)draw.text((x, y), char, fill=(random.randint(0, 100), random.randint(0, 100), random.randint(0, 100)),font=font)# 添加干扰线for _ in range(3):x1 = random.randint(0, width)y1 = random.randint(0, height)x2 = random.randint(0, width)y2 = random.randint(0, height)draw.line((x1, y1, x2, y2), fill=(random.randint(150, 200), random.randint(150, 200), random.randint(150, 200)),width=1)# 添加噪点for _ in range(50):x = random.randint(0, width - 1)y = random.randint(0, height - 1)draw.point((x, y), fill=(random.randint(150, 200), random.randint(150, 200), random.randint(150, 200)))# 将图片转换为二进制数据buffered = BytesIO()image.save(buffered, format="JPEG", quality=95)  # 明确指定质量和格式return image
http://www.rkmt.cn/news/17720.html

相关文章:

  • CentOS 7 一键安装 vsftpd 并创建可登录 FTP 用户 test - 教程
  • 破解工地防盗难题:如何利用国标GB28181视频平台EasyCVR实现视频监控统一管理?
  • autogen论文解读 - Sun
  • 高效仿真:功耗与散热攻略
  • # 中国大模型落地应用研究报告2025 - 深度导读与趋势分析
  • 车企数据治理平台化实战:从数据孤岛到全链路治理的架构演进
  • 完整教程:Java中的缓存机制与分布式缓存实现!
  • jsconfig.json-vscode或cursor ctrl点击@路径,快速到达
  • 完整教程:经典字符串与数组题目
  • 完整教程:Real-Time MDNet
  • AutoCAD 2025 CAD 安装包中文永久免费免激活破解版下载 附图文安装教程
  • nmcli修改ip地址
  • 从C到pwn入门
  • for循环s.length()-1,s为空时的一直执行循环的问题
  • 一文读懂AI Agent:为什么说它是大模型的下一站?
  • AI元人文构想的新启发:从自动驾驶困境到通用价值智能的构建——声明Ai研究
  • mido配置 DNS 服务器
  • Flutter 中运用 Color 的最优方案
  • 竞争自适应重加权采样(CARS)算法在光谱数据变量选择中的解决方案
  • AI元人文构想的新启发:从自动驾驶困境到通用价值智能的构建
  • Word通过宏统一设置样式
  • Origin 2025b安装包下载及详细安装教程,附永久免费中文汉化破解版Origin安装包
  • st表模板
  • 详细介绍:百度Qianfan-VL系列上线:推出3B/8B/70B三款视觉理解模型,覆盖不同算力需求
  • CesiumGlobeAnchor
  • 技术复习要点清单
  • res-downloader v2.1.2 全平台资源下载工具深度指南:支持视频号/抖音/音视频嗅探,附常见问题解决方案
  • 6G多站多智能超表面(RIS)
  • 缓冲区管理
  • 智慧考试微信小程序系统:一站式在线考试解决方案