This example of this article for you to share the code of Python image authentication codes for your reference, the specific content as follows
#!/usr/bin/env python #-*-coding:utf-8-*-import random from PiL import Image, Imagedraw, Imagefont, ImageFilter Try:import Cstringio as Stringio except importerror:import stringio _letter_cases = "Abcdefghjkmnpqrstuvwxy" # lowercase Letter _upper_cases = "Abcdefghjklmnpqrstuvwxy" # Capital Letter _numbers = "1234567890" # Number I Nit_chars = '. Join ((_letter_cases, _upper_cases, _numbers)) # Generate allowed character Set combination Default_font = "./dejavusans.ttf" # Authenticode Font # Generate Verification Code Interface Def generate_verify_image (size=, Chars=init_chars, img_type= "GIF) ", mode=" RGB ", bg_color= (255, 255, 255), fg_color= (0, 0, 255), Font_si Ze=18, Font_type=default_font, length=4, Draw_lines=true, n_line= (1, 2 ), Draw_points=true, point_chance=2, Save_img=false): "" "Generate a Captcha picture:p Aram Si Ze: Picture size, format (wide, high), default to (120):p Aram Chars: Allowed character set, format string:p Aram Img_type: Picture save format, default to GIF, optional gif,jpeg,tiff,png:p aram mode: Picture mode, default to RGB :p Aram Bg_color: Background color, default to white:p Aram Fg_color: foreground color, authenticode character color, default to Blue #0000ff:p Aram font_size: Authenticode font size:p Aram Font_type: Validating Code font, default to Dejavusans.ttf:p Aram Length: The number of captcha characters:p Aram Draw_lines: whether to row the interference line:p Aram N_line: The line of interference line range, format tuple, default is (1, 2), only DRA W_lines is true:p Aram Draw_points: Whether to draw the jamming point:p Aram Point_chance: The probability of the disturbance point appearing, the size range [0]:p Aram save_img: Save as Picture: RE
Turn: [0]: Verification code byte throttle, [1]: The string in the verification code picture "" "width, height = size # width, high img = image.new (mode, size, bg_color) # Create graphics Draw = Imagedraw.draw (img) # Create Brush def get_chars (): "" to generate a string of a given length, return to the list format "" "" "" "returns Random.sample (chars, Lengt h) def create_lines (): "" "Draw the Noise Line" "" Line_num = Random.randint (*n_line) # interference line number for I in range (Line_num) : # start point begin = (Random.randint (0, size[0]), Random.randint (0, size[1])) # End point = (Random.randi NT (0, size[0]), RANDOM.RAndint (0, size[1]) draw.line ([begin, end], fill= (0, 0, 0)) def create_points (): "" Draw interference Point "" "Chance = MIN (0, int (point_chance))) # Size limited to [0] for W xrange (width): for h in xrange (height): t MP = Random.randint (0) if tmp > 100-chance:draw.point ((w, h), fill= (0, 0, 0)) def Create_ STRs (): "" Draw Verification Code character "" C_chars = Get_chars () STRs = '%s '% '. Join (C_chars) # before and after each character font = Ima Gefont.truetype (Font_type, font_size) font_width, font_height = Font.getsize (STRs) draw.text ((Width-font_widt h)/3, (Height-font_height)/3), STRs, Font=font, Fill=fg_color) return '. Join (c_chars) if Draw_li Nes:create_lines () if draw_points:create_points () STRs = Create_strs () # graphics warp parameter params = [1-float (RA Ndom.randint (1, 2))/0, 0, 0, 1-float (random.randint (1))/, float (rando
M.randint (1, 2))/500, 0.001, float (random.randint (1, 2))/+ img = img.transform (size, image.perspective, params) # Chuang Build twisted img = Img.filter (imagefilter.edge_enhance_more) # Filter, boundary enhancement (threshold greater) Mstream = Stringio.stringio () img.save (mstre AM, Img_type) if Save_img:img.save ("Validate.gif", Img_type) return mstream, strs if __name__ = "__main_ _ ": mstream, STRs = Generate_verify_image (save_img=true) Print STRs
The above is the entire contents of this article, I hope to learn from the Python program to help.