Using python to generate random image captcha, need to use the pillow module
1. Install the pillow module
pip install pillow
Basic Use of Modules
1.Create a picture
from PIL import Image # Define the use of the Image class to instantiate a 400px long, 400px wide, RGB-based (255,255,255) color image. img1=(mode="RGB",size=(400,400),color=(255,255,255)) # Save the generated image as "" format with open("","wb") as f: (f,format="png") #Display Pictures ()
Run the program, the program will be in the py file at the same level under the generation of a small picture named "", the picture length of 400px, width of 400px, the color of white.
2. Create brushes
# Create brushes for generating content on images. draw1=(img1,mode="RGB")
3. Generate points on the picture
# Generate a red dot at (100,100) coordinates, the specified coordinates must not exceed the size of the image. ([100,100],pill="red") # Generate a black point at (80,80) coordinates, the specified coordinates must not exceed the size of the image. ([80,80],fill=(0,0,0))
4. Draw lines on the picture
#The first bracketed parameter is the coordinates, the first two numbers are the start coordinates, and the last two numbers are the end coordinates. #The second parameter in parentheses specifies the color, which can be specified directly or in RGB. ((100,100,100,300),fill="red") ((100,200,200,100),fill="blue")
Run the program, the brush will draw a red vertical line between (100,100) and (100,300) coordinates, and a blue diagonal line between (100,200) and (200,100) coordinates.
5. Draw a circle in the picture
#The first parameter in parentheses is the coordinates, the first two numbers are the start coordinates, the last two are the end coordinates. # Generate a circle from the square area between these two coordinates, the second parameter in curly brackets is the angle at which the circle starts. #The third parameter is the end angle of the circle, 0 to 360 means the circle is a complete circle. # can also specify the number to generate a section for the arc, the last parameter indicates the color, you can also use RGB to indicate the desired color ((100,100,300,300),0,360,fill="red") ((0,0,300,300),0,90,fill="blue")
6. Writing text in pictures
# Generate text on an image using the text method of the brush # The first parameter is the coordinate, the second parameter is the content of all the generated text. # The third parameter is the color of the text ([0,0],"python","blue")
7. In the picture in the generation of the specified font text
# Instantiate a font object, the first parameter is the path to the font, the second parameter is the font size. font1=("One ",28) # Generate fonts on images #The first parameter in parentheses indicates the coordinates, the second parameter indicates the content to be written. #The third parameter indicates the color, the fourth parameter indicates the font object used. ([200,200],"linux","red",font=font1)
Example of an image captcha
# Import the random module import random # Import Image, ImageDraw, ImageFont modules. from PIL import Image,ImageDraw,ImageFont # Define the use of the Image class to instantiate a 120px long, 30px wide, RGB-based (255,255,255) color image. img1=(mode="RGB",size=(120,30),color=(255,255,255)) # Instantiate a paintbrush draw1=(img1,mode="RGB") # Define the fonts to be used font1=("One ",28) for i in range(5): # Every time you loop, generate a random letter or number from a to z. #65 to 90 for the letters of the ASCII code, the use of chr to generate the ASCII code into characters #str Converts the generated number to a string. char1=([chr((65,90)),str((0,9))]) # Regenerate random colors every cycle color1=((0,255),(0,255),(0,255)) # Add the generated letters or numbers to the picture #Image length is 120px, to generate 5 numbers or letters, every time you add one, its position should be moved back 24px. ([i*24,0],char1,color1,font=font1) # Save the generated image as "" format with open("","wb") as f: (f,format="png")
Each time you run it, the program generates a small image containing random characters in the same directory as the program.
summarize
Above is this article on python generate random graphic CAPTCHA detailed all, I hope to help you. Interested friends can continue to refer to this site:Python crawler example crawl website funny paragraph、Python Beginner's Trigonometric Functions Explained [Favorite、Python basic exercises of user login implementation code sharingetc., what can always leave a message, I will reply to everyone in a timely manner. Thank you friends for the support of this site!