py生成图片验证码 发表于 2018-04-06 | | 阅读次数: 生成图片二维码,并判断填写的验证码是否正确。1234567891011121314151617181920212223242526272829import randomimport stringimport sysfrom PIL import Image,ImageDraw,ImageFontdef random_text (): #生成一串4位的随机字符串 text= string.digits+ string.ascii_letters str= '' for i in range(0, 4): str= str+ text[random.randint(0, len(text))] return strdef get_code (str): width= 120 height= 60 #设置图片大小 color= (43, 34, 88) #设置图片颜色 image= Image.new('RGB', (width, height), color) #创建图片 fontl= ImageFont.truetype("simsun.ttc", 40, index= 1) #设置字体样式 draw= ImageDraw.Draw(image) #将验证码画到图片上 draw.text((20, 10), str, fill=(355, 0, 0), font= fontl) image.show()s= random_text()get_code(s)code= input("请输入图片中的验证码: ")if s.lower()== code.lower(): print("验证码输入正确")else: print ("验证码输入错误") 运行结果如图所示: Your support will encourage me to continue to create! Donate WeChat Pay Alipay