Joke Collection Website - Blessing messages - How is the function of python verification code timestamp realized?
How is the function of python verification code timestamp realized?
Random import
Import image from PIL, image drawing, image font, image filter.
_ letter _ cases = "abcdefghjkmnpqrstuwxy" # lowercase letters, remove I, L, O, Z that may interfere.
_ upper _ cases = _ letter _ cases.upper () # capital letters
_ numbers =“”。 Join (map (str, range (3, 10)) # number
init_chars =“”。 Join((_letter_cases, _ capital, _numbers))
def create _ validate _ code(size =( 120,30),
chars=init_chars,
img_type="GIF ",
mode="RGB ",
bg_color=(255,255,255),
fg_color=(0,0,255),
font_size= 18,
font_type="Monaco.ttf ",
Length =4,
draw_lines=True,
n_line=( 1,2),
draw_points=True,
point_chance=2):
"""
@todo: generate verification code picture
@param size: the size and format (width and height) of the picture. The default value is (120,30).
@param chars: allowed character set, format string.
@param img_type: save the format of the picture. The default is GIF, and the options are GIF, JPEG, TIFF and PNG.
@param mode: picture mode, which is RGB by default.
@param bg_color: background color, which is white by default.
@param fg_color: foreground color, character color of verification code, and blue #0000FF by default.
@param font_size: font size of verification code.
@param font_type: the font of the verification code; the default is AE _ alaarabiya.ttf.
@param length: the number of characters of the verification code.
@param draw_lines: Do you want to draw interference lines?
@param n_lines: number range of interference lines, format tuple, which is (1, 2) by default, and only valid when draw_lines is True.
@param draw_points: whether to draw interference points.
@param point_chance: probability of interference point, size range [0, 100]
@return: [0]: PIL mirror instance
@return: [1]: the string in the verification code picture.
"""
Width, Height = Size # Width and Height
# Create graphics
Img = Image.new (mode, size, background color)
Draw = ImageDraw。 Draw (img) # Create a brush
def get_chars():
"""Generates a string of a given length and returns a list format" ""
Returns random.sample (character, length)
Define create_lines ():
"""Draw interference lines" ""
Line _ num = random. Randint (* n _ line) # Number of interference lines
For I (line number) in range:
# Starting point
begin = (random.randint(0,size[0]),random.randint(0,size[ 1]))
# Endpoint
end = (random.randint(0,size[0]),random.randint(0,size[ 1]))
draw.line([begin,end],fill=(0,0,0))
Define creation points ():
"""Draw interference points" ""
Chance = min (100, max (0, int (point _ chance)) # Size is limited to [0, 100].
For w (width) in the range:
For h (height) in the range:
tmp = random.randint(0, 100)
If tmp> 100-opportunity:
draw.point((w,h),fill=(0,0,0))
def create_strs():
"""Draw verification code characters" ""
c_chars = get_chars()
Strs =' %s'% '。 Join(c_chars) # Each character is separated by a space.
# font = image font . truetype(font _ type,font_size)
font = ImageFont.load_default()。 script
font_width,font_height = font.getsize(strs)
draw . text((width-font _ width)/3,(height - font_height) / 3),
strs,font=font,fill=fg_color)
Return "". Join (c_chars)
If you draw a line:
Create _ Line ()
If you draw a _ point:
Create Points ()
strs = create_strs()
# Graphic distortion parameter
params =[ 1-float(random . randint( 1,2)) / 100,
0,0,0, 1-float(random.randint( 1, 10)) / 100,float(random.randint( 1,2)) / 500,0.00 1,float(random . randint( 1,2)) / 500 ]
Img = img.transform (size, image.perspective, params) # Create a warp.
Img = img.filter (imagefilter.edge _ enhance _ more) # filter, with enhanced boundary (larger threshold).
- Previous article:Heping jingying handwritten newspaper
- Next article:Mobile phone short message virus painting
- Related articles
- Simple greetings at noon, warm greetings at noon.
- Zhengzhou’s latest travel policy regarding the epidemic: Can Zhengzhou now come and go freely? - Wan Nian Calendar
- How to apply for 189 mailbox?
- Mother's congratulatory letter on her daughter's marriage.
- Complete works of short sentences of Mid-Autumn Festival greetings.
- How to add verification code in Easy Language
- Daughter's birthday wishes to mother.
- How to write a wedding message?
- Will the money be refunded if the ticket grab fails?
- Watch the 300-word composition of Ayang primary school science and technology flow officer