Detailed description of random graphic verification codes generated by python, and detailed description of python verification Codes

Source: Internet
Author: User

Detailed description of random graphic verification codes generated by python, and detailed description of python verification Codes

Use the pillow module to generate a random image verification code using python.

1. Install the pillow Module

pip install pillow

2. Basic use of the pillow Module

1. Create an image

From PIL import Image # defines the use of the Image class to instantiate an Image of a length of 400px, a width of 400px, and an Image of the RGB (255,255,255) color img1 = Image. new (mode = "RGB", size = (400,400), color = (255,255,255) # Save the generated image as "pic.png" format with open ("pic.png ", "wb") as f: img1.save (f, format = "png") # display the image img1.show ()

Run the program. The program will generate a small image named "pic.png" under the same level of The py file. The image length is 400px, the width is 400px, and the color is white.

2. Create a paint brush

# Create a paint brush to generate the content draw1 = ImageDraw. Draw (img1, mode = "RGB") on the image ")

3. Generate a vertex on the Image

# Generate a red dot on the (100,100) Coordinate. The specified coordinate cannot exceed the image size draw1.point ([100,100], pill = "red") # In (80, 80) the specified coordinates cannot exceed the size of the image draw1.point ([80, 80], fill = (0, 0, 0 ))

4. Draw a line on the Image

# The parameters in the first bracket are coordinates, the first two are the start coordinates, and the last two are the end coordinates. # The second parameter in the brackets specifies the color, which can be directly specified, you can also use RGB to represent the colors draw1.line (100,100,100,300), fill = "red") draw1.line (100,200,200,100), fill = "blue ")

Run the program. The paint brush draws a red vertical line between the coordinates (100,100) and (100,300), and draws a blue diagonal line between the coordinates (100,200) and (200,100 ).

5. Circle the image

# The first parameter in parentheses is the coordinate, the first two are the starting coordinate, and the last two are the ending coordinate # generate a circle using the square area between the two coordinates, the second parameter in braces is the starting angle of the circle # The third parameter is the ending angle of the circle. 0 to 360 indicates that the circle is a complete circle, # You can also specify a number to generate an arc. The last parameter indicates the color. You can also use RGB to represent the desired color draw1.arc (100,100,300,300), 0,360, fill = "red") draw1.arc (300,300,), fill = "blue ")

6. Write text in the image

# Use the text method of the paint brush to generate text on the image # The first parameter is the coordinate, the second parameter is the content of all generated text # The third parameter is the text color draw1.text ([0, 0], "python", "blue ")

7. Generate text in the specified font in the image

# Instantiate a font object first. The first parameter indicates the font path, and the second parameter indicates the font1 = ImageFont font size. truetype ("One Chance. ttf ", 28) # generate a font on the image # parameters in the first bracket represent coordinates, and the second parameter represent written content # The third parameter represents color, the fourth parameter indicates the used font object draw1.text ([200,200], "linux", "red", font = font1)

Image Verification Code instance

# Import random module import random # import Image, ImageDraw, ImageFont module from PIL import Image, ImageDraw, ImageFont # define to instantiate an Image class with a length of 120px and a width of 30px, image img1 = Image in RGB (255,255,255) color. new (mode = "RGB", size = (255,255,255), color = () # instantiate a paint brush draw1 = ImageDraw. draw (img1, mode = "RGB") # define the font1 = ImageFont font to be used. truetype ("One Chance. ttf ", 28) for I in range (5): # An ASCII code consisting of letters or numbers #65 to 90 is randomly generated from a to z every cycle, use chr to convert the generated ASCII code into characters # str to convert the generated number into a string char1 = random. choice ([chr (random. randint (65,90), str (random. randint ()]) # regenerate the random color color1 = (random. randint (0,255), random. randint (0,255), random. randint (0,255) # Add the Generated letters or numbers to the image # The image length is 120px. To generate five numbers or letters, add one at a time, the position is to move 24px draw1.text ([I * 24, 0], char1, color1, font = font1) backward. # Save the generated image as "pic.png" in the format of with open ("pic.png ", "wb") as f: img1.save (f, format = "png ")

Each time the program runs, a small image containing random characters will be generated in the program directory at the same level.

Summary

The above is all the details about how to generate a random graphic verification code in python. I hope it will be helpful to you. If you are interested, you can continue to refer to this site: Python crawler instance crawling funny website jokes, Python getting started trigonometric function full solution [favorites], Python basic exercises User Login implementation code sharing, etc, if you can leave a message at any time, the editor will reply to you in a timely manner. Thank you for your support!

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.