鱼C论坛

 找回密码
 立即注册
查看: 1752|回复: 3

[已解决]求解!!!!!!

[复制链接]
发表于 2017-10-14 13:55:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如何用pil做一个验证码的程序    需要把验证码的数字输出          需要掌握那些python知识
最佳答案
2017-10-14 14:59:31
你是不是想做一个图片验证码,
from PIL import Image,ImageDraw,ImageFont,ImageFilter
import os,random,string
用这些模块就可以。网上能好到已经写好的代码。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-10-14 14:51:10 | 显示全部楼层
基本语法和相应的模块即可
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-14 14:59:31 | 显示全部楼层    本楼为最佳答案   
你是不是想做一个图片验证码,
from PIL import Image,ImageDraw,ImageFont,ImageFilter
import os,random,string
用这些模块就可以。网上能好到已经写好的代码。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-14 15:01:26 | 显示全部楼层
本帖最后由 ooxx7788 于 2017-10-14 15:08 编辑

直接去找网上写好的代码最快。
比如用我这个。



  1. from PIL import Image, ImageDraw, ImageFont
  2. import random, numpy, string, os
  3. from multiprocessing import Pool

  4. def check_dirs():
  5.     dir_path = r"E:\\checkcode"
  6.    
  7.     if not os.path.exists(dir_path):
  8.         os.makedirs(dir_path)
  9.     return dir_path


  10. def gen_checkcode(dir_path, n=4):
  11.     if os.listdir(dir_path):
  12.         filenname = str(max([int(f_names.split('.')[0].split('-')[0]) for f_names in os.listdir(dir_path)]) + 1)
  13.         # filenname = random.randint(1, 10000)
  14.     else:
  15.         filenname = 0
  16.    
  17.     text = random.sample(string.ascii_letters, n)
  18.     # print(text)
  19.    
  20.     rawArray = numpy.zeros((100, 300, 3), dtype=numpy.uint8)
  21.     sh = rawArray.shape
  22.     for i in range(sh[0]):
  23.         for j in range(sh[1]):
  24.             for k in range(sh[2]):
  25.                 rawArray[i][j][k] = random.randint(0, 255)
  26.    
  27.     im = Image.fromarray(rawArray)
  28.     draw = ImageDraw.Draw(im)
  29.    
  30.     for i in range(len(text)):
  31.         draw.text(((300 / n) * i + random.randint(0, 40), random.randint(0, 40)), text[i],
  32.                   font=ImageFont.truetype(r"C:\Windows\Fonts\arial.ttf", 40),   # 字体
  33.                   fill=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
  34.    
  35.     im.save(dir_path + f"/{''.join(text)}.jpg")


  36. def main():
  37.     dir_path = check_dirs()
  38.     pages, l = 10, 4

  39.    
  40.     pool = Pool(processes=4)
  41.     for i in range(pages):
  42.         pool.apply_async(gen_checkcode, (dir_path, l,))
  43.     pool.close()
  44.     pool.join()


  45. if __name__ == '__main__':
  46.     main()
复制代码

QQ图片20171014150658.png
我自己都看不清的验证码。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-3 02:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表