Python image verification code generation,
This example shares the python image verification code for your reference. The details are as follows:
#! /Usr/bin/env python #-*-coding: UTF-8-*-import randomfrom PIL import Image, ImageDraw, ImageFont, ImageFilter try: import cStringIO as stringiow.t ImportError: import StringIO _ letter_cases = "abcdefghjkmnpqrstuvwxy" # lowercase letter _ upper_cases = "numbers" # uppercase letter _ numbers = "1234567890" # Number init_chars = ''. join (_ letter_cases, _ upper_cases, _ numbers) # generate the allowed character set default_font = ". /DejaVuSans. ttf "# Verification Code font # generate Verification Code interface def generate_verify_image (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 = default_font, length = 4, draw_lines = True, n_line = (1, 2 ), draw_points = True, point_chance = 2, save_img = False): "generate Verification Code image: param size: image size, format (width, height), default value: (120, 30): param chars: allowed character set combination. Format String: param img_type: Format of image storage. Default Value: GIF. Optional values: GIF, JPEG, TIFF, PNG: param mode: image mode. Default Value: RGB: param bg_color: background color. Default Value: White: param fg_color: foreground color, verification code Character Color. Default Value: Blue # 0000FF: param font_size: Verification Code font size: param font_type: font of the verification code. The default value is DejaVuSans. ttf: param length: number of characters in the verification code: param draw_lines: whether to draw interference lines: param n_line: The number range of interference lines, format tuples, default value: (1, 2 ), valid only when draw_lines is True: param draw_points: whether to draw interference points: param point_chance: the probability of interference points, the size range is [0,100]: param save_img: whether to save as an image: return: [0]: Verification Code byte stream, [1]: the string "width, height = size # width, height img = Image in the verification code Image. new (mode, size, bg_color) # create a graphic draw = ImageDraw. draw (img) # create brush def get_chars (): "generate a string of the given length, return the List format" "return random. sample (chars, length) def create_lines (): "Draw interference line" line_num = random. randint (* n_line) # Number of interfering lines for I in range (line_num): # Starting Point begin = (random. randint (0, size [0]), random. randint (0, size [1]) # end Point end = (random. randint (0, size [0]), random. randint (0, size [1]) draw. line ([begin, end], fill = (0, 0, 0) def create_points (): "" chance = min (100, max (0, int (point_chance) # The size is limited to [0,100] for w in xrange (width): for h in xrange (height): tmp = random. randint (0,100) 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) # separate each character with spaces. font = ImageFont. truetype (font_type, font_size) 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 draw_lines: create_lines () if draw_points: 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.001, float (random. randint (1, 2)/500] img = img. transform (size, Image. PERSPECTIVE, params) # create distortion img = img. filter (ImageFilter. EDGE_ENHANCE_MORE) # filter, boundary enhancement (greater threshold) mstream = StringIO. stringIO () img. save (mstream, 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 all the content of this article. I hope it will help you learn python programming.