Python generates random verification code

Source: Internet
Author: User

Python generates a random verification code that requires the use of the PIL module.

Installation:

PIP3 Install Pillow
Basic use

1. Create a picture

From PIL Import imageimg = image.new (mode= ' RGB ', size= (+), color= (255, 255, 255)) # Open in Picture Viewer # Img.show ()  # saved in this Ground with open (' Code.png ', ' WB ') as F:    Img.save (f,format= ' png ')

2. Create a brush to draw any content on the picture

img = image.new (mode= ' RGB ', size= (+), color= (255, 255, 255)) Draw = Imagedraw.draw (img, mode= ' RGB ')

3. Draw points

img = image.new (mode= ' RGB ', size= (+), color= (255, 255, 255)) Draw = Imagedraw.draw (img, mode= ' RGB ') # first parameter: Indicates the coordinate # second parameter Number: Denotes color draw.point ([+], fill= "Red") draw.point ([+], fill= (255, 255, 255))

4. Draw the Line

img = image.new (mode= ' RGB ', size= (+), color= (255, 255, 255)) Draw = Imagedraw.draw (img, mode= ' RGB ') # first parameter: represents the starting coordinate and the end Coordinates # SECOND parameter: Indicates the color Draw.line ((100,100,100,300), fill= ' Red ') Draw.line ((100,100,300,100), fill= (255, 255, 255))

5. Draw a Circle

img = image.new (mode= ' RGB ', size= (+), color= (255, 255, 255)) Draw = Imagedraw.draw (img, mode= ' RGB ') # first parameter: represents the starting coordinate and the end Coordinates (circle to draw in) # second argument: Represents the start angle # third parameter: Indicates end angle # Fourth parameter: Indicates color Draw.arc ((100,100,300,300), 0,90,fill= "Red")

6. Writing text

img = image.new (mode= ' RGB ', size= (+), color= (255, 255, 255)) Draw = Imagedraw.draw (img, mode= ' RGB ') # first parameter: Indicates the starting coordinate # second Parameters: Indicates write content # third parameter: Indicates color Draw.text ([0,0], ' python ', "Red")

7. Special Font text

img = image.new (mode= ' RGB ', size= (+), color= (255, 255, 255)) Draw = Imagedraw.draw (img, mode= ' RGB ') # first parameter: Indicates the font file path # Second parameter: Font size = Imagefont.truetype ("Kumo.ttf", 28) # First parameter: represents the starting coordinate # The second parameter: indicates the Write content # The third parameter: represents the color # Fourth parameter: Represents a color Draw.text ([0 , 0], ' python ', ' Red ', Font=font)
Image Verification Code
Import Random def check_code (width=120, height=30, char_length=5, font_file= ' Kumo.ttf ', font_size=28): Code = [] img = Image.new (mode= ' RGB ', size= (width, height), color= (255, 255, 255)) Draw = Imagedraw.draw (img, mode= ' RGB ') def RN Dchar (): "" "generates random letters: return:" "" Return Chr (Random.randint (+)) def Rndcolor (): "" "generates Random Color: return:" "" Return (random.randint (0, 255), Random.randint (255), RA        Ndom.randint (64, 255)) # Write Text font = Imagefont.truetype (Font_file, font_size) for I in Range (char_length): char = Rndchar () code.append (char) h = random.randint (0, 4) draw.text ([i * width/char_length, H], Char, Font=font, Fill=rndcolor ()) # Write interference point for I in range (+): Draw.point ([Random.randint (0, width), random.ra Ndint (0, height)], Fill=rndcolor ()) # Write interference circle for I in range (+): Draw.point ([Random.randint (0, width), random . randint (0, height)], fiLl=rndcolor ()) x = Random.randint (0, width) y = random.randint (0, height) draw.arc ((x, y, x + 4, y + 4), 0, Fill=rndcolor, ()) # Draw interference line for I in range (5): x1 = Random.randint (0, width) y1 = Random.randi NT (0, height) x2 = random.randint (0, width) y2 = random.randint (0, height) draw.line ((x1, y1, x2, y2 ), Fill=rndcolor ()) img = Img.filter (imagefilter.edge_enhance_more) return IMG, '. Join (code) if __name__ = = ' __mai N__ ': # 1. Directly open # Img,code = Check_code () # img.show () # 2. Write File # Img,code = Check_code () # with open (' Code.png ', ' WB ') as F: # Img.save (f,format= ' PNG ') # 3.     Write Memory (Python3) # from IO import Bytesio # stream = Bytesio () # Img.save (stream, ' png ') # Stream.getvalue () # 4.     Write Memory (Python2) # import Stringio # stream = Stringio.stringio () # Img.save (stream, ' png ') # Stream.getvalue () Pass

Note: font file download, Bash here

Python generates random verification code

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.