Python generates CAPTCHA instances

Source: Internet
Author: User
#!/usr/bin/env python#coding=utf-8import randomimport Image, Imagedraw, Imagefont, imagefilter _letter_cases = " Abcdefghjkmnpqrstuvwxy "# lowercase letters, removing potentially disturbing i,l,o,z_upper_cases = _letter_cases.upper () # uppercase letters _numbers = '. Join (Map (str, Range (3, 10)) # number Init_chars = '. Join ((_letter_cases, _upper_cases, _numbers)) fonttype= "/usr/share/fonts/truetype/                             Freefont/freesans.ttf "Def create_validate_code (size=), Chars=init_chars, Img_type= "GIF", mode= "RGB", bg_color= (255, 255, 2                             , fg_color= (0, 0, 255), font_size=18,                             Font_type=fonttype, length=4, Draw_lines=true, N_line= (1, 2), draw_points=true, point_chance = 2): ' "@todo: Generate Captcha picture @param size: Picture size, format (wide, high), the default is (+), @param chars: The allowed character set, format string @param img_type: The format of the picture save, the default is GIF, optional is gif,jpeg,tiff,png @param mode: The default is R GB @param bg_color: Background color, default is white @param fg_color: foreground color, captcha character color, default is blue #0000ff @param font_size: captcha font size @param font_type: Test Code font, the default is Ae_alarabiya.ttf @param length: The number of verification code characters @param Draw_lines: whether the interference line @param n_lines: The number of interference lines, format tuples, the default is (1, 2), only Dr Aw_lines valid @param draw_points: whether to draw the disturbance point @param point_chance: Probability of the occurrence of the disturbance point, size range [0] @return: [0]: PIL Image instance @retu RN: [1]: The string "width in the captcha picture, height = size # wide, high img = image.new (mode, size, bg_color) # Create graphic draw = Imagedraw.dr AW (IMG) # Create brush if Draw_lines:create_lines (draw,n_line,width,height) if Draw_points:create_points (Draw,point_chan Ce,width,height) STRs = Create_strs (Draw,chars,length,font_type, Font_size,width,height,fg_color) # graphics warp parameter params = [ 1-float (Random.randint (1, 2))/0, 0, 0, 1-float (random.randint (1, 10 )/, FloAt (Random.randint (1, 2))/$, 0.001, float (random.randint (1, 2)/+) img = Img.tra Nsform (Size, image.perspective, params) # Create twist img = Img.filter (imagefilter.edge_enhance_more) # Filter, Edge Enhancement (threshold value) retur n img, STRs def create_lines (draw,n_line,width,height): ' Draw interference line ' ' Line_num = Random.randint (n_line[0],n_line[1]) # Number of interference lines for I in range (line_num): # start point begin = (Random.randint (0, width), random.randint (0, height)) #结束点 end = (Random.randint (0, width), random.randint (0, height)) draw.line ([begin, end], fill= (0, 0, 0)) def create_points (draw , point_chance,width,height): ' Draw disturbance point ' chance = min (max (0, Int (point_chance))) # size limit at [0, +] for W in Xrang E (width): for h in xrange (height): tmp = Random.randint (0, +) if tmp > 100-chance:draw.point (( W, h), fill= (0, 0, 0)) def create_strs (Draw,chars,length,font_type, Font_size,width,height,fg_color): "' Draw the captcha character '" Generates a string of the given length, returning the list format ' C_chars = Random.sample (chars, length) STRs = '%s '% '. Join (C_chars) # Each character is separated by a space before and after the font = Imagefont.truetype (font_t Ype, Font_size) font_width, font_height = Font.getsize (STRs) Draw.text (((width-font_width)/3, (Height-font_heigh T)/3), STRs, Font=font, Fill=fg_color) return ". Join (c_chars) if __name__ = =" __main__ ": code_img = Create_val Idate_code () code_img[0].save ("Validate.gif", "gif") print code_img[1]
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.