程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Automatic generation of verification code for Python tool series

編輯:Python

Python The verification code of the tool series is automatically generated

The applet crawler receives the order 、app Crawler receiving order 、 Web crawlers receive orders 、 Interface customization 、 Website development 、 Applet development > Click here to contact us <

Please scan the QR code below for wechat

The code is for learning and communication only , Do not use for illegal purposes

Go straight to the code

# -*- coding:utf-8 -*-
import random
import os
from PIL import Image, ImageDraw, ImageFont
# Gap size between each letter
fontClearance = 2
# diameter C
arcN = 0
def getRandColor():
'''
Get random rgb Color tuple
:return:
'''
return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
def getRandStr():
'''
Get random character 0-9 a-z
:return:
'''
return random.choice([chr(random.randint(65, 90)), str(random.randint(0, 9))])
def getRangBgImg():
'''
From the folder where the current program is located imgs Get a random picture under the folder
:return:
'''
currPath = os.getcwd() + "/imgs"
fs = os.listdir(currPath)
return currPath + "/" + random.choice(fs)
def getFontSize(n, width, height):
'''
Get the font size
:param n: Number of Fonts
:param width: Image width
:param height: Picture height
:return:
'''
global arcN
fontAllClearance = 2 * n * fontClearance
reClearance = width - fontAllClearance
fontSize = reClearance // n
startY = 0
if fontSize < height:
startY = (height - fontSize) // 2
else:
fontSize = height - 10
startY = 5
startX = fontClearance
arcN = fontSize // 10
return fontSize, startX, startY
def getRandLine(width, height):
'''
Get random lines
:param width: Image width
:param height: Picture height
:return:
'''
return random.randint(0, width), random.randint(0, height), random.randint(0, width), random.randint(0, height)
def getRandPoint(width, height):
'''
Obtain the square diagonal coordinates of random points
:param width: Image width
:param height: Picture height
:return:
'''
x = random.randint(0, width)
y = random.randint(0, height)
return x, y, x + arcN, y + arcN
def genValidCode(strNum, lineNums, pointNums):
'''
Verification code generation function
:param strNum: Number of letters of verification code
:param lineNums: Number of verification code lines
:param pointNums: Number of verification code circles
:return:
'''
# Get the file path of a random picture
imgPath = getRangBgImg()
# Open the picture object
img = Image.open(imgPath)
# Gets the width and height of the image
width, height = img.size
# By the number of fonts and the width and height of the picture , Calculate the font size , And when the font starts writing x y coordinate
fontSize, startX, startY = getFontSize(strNum, width, height)
# Instantiate a paintbrush
draw1 = ImageDraw.Draw(img, mode="RGB")
# Define the font to use
font1 = ImageFont.truetype("simfang.ttf", fontSize)
# Write
for i in range(strNum):
s = getRandStr()
draw1.text([startX + (i * fontSize) + (((2 * (i + 1)) - 1) * fontClearance), startY], s, getRandColor(), font=font1)
# Draw line
for i in range(lineNums):
draw1.line((getRandLine(width, height)), fill=getRandColor())
# Draw a dot
for i in range(pointNums):
draw1.arc((getRandPoint(width, height)), 0, 360, fill=getRandColor())
# Generate pictures
with open("pic.png", "wb") as f:
img.save(f, format="png")
print(" Generate successfully !")
if __name__ == '__main__':
# Initialization captcha 、 Number of lines 、 Number of circles
strNum, lineNums, pointNums = 5, 5, 10
genValidCode(strNum, lineNums, pointNums)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved