Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Python to randomly generate Picture Verification Code

Shulou Source: shulou.com Published: 2022-06-02 01:33:25 10月30日 Update

This article mainly explains "how to use Python to randomly generate picture verification code", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to use Python to randomly generate picture verification code"!

Import module

Import randomfrom PIL import Image,ImageFont,ImageDraw

Generate random verification code

Def rndtxt (): txt_list = [] # uppercase letter txt_list.extend ([i for i in range (65 txt_list 90)]) # lowercase letter txt_list.extend ([i for i in range (97123)]) # numeric txt_list.extend ([i for i in range (48,557)]) return chr (txt_ list.Randint (0LN (txt_list)-1)])

As the background color and font color of the picture to be generated later.

Def rndbgcolor (): # background color return (random.randint (64255), random.randint (64255), random.randint (64255)) def rndtxtcolor2 (): # font color return (random.randint (32127), random.randint (32127), random.randint (32127))

Generate the print CAPTCHA and open it in the form of a picture. Do not save the picture file, but only for temporary CAPTCHA verification

Def code (): weight = 240hight = 60 image = Image.new ('RGB', (weight,hight), (255255255)) font = ImageFont.truetype (' msyh.ttc',36) draw = ImageDraw.Draw (image) # fill background color for x in range (weight): for y in range (hight): draw.point ((xQuery) Fill=rndbgcolor () # generate random verification code for T in range (4): rndtxt_2 = rndtxt () print (rndtxt_2) # print the value of the verification code draw.text ((60 * T + 10), rndtxt_2,font=font,fill=rndtxtcolor2 ()) image.show ()

Complete code:

#! / usr/bin/env python3import randomfrom PIL import Image,ImageFont,ImageDraw def rndtxt (): txt_list = [] # uppercase letter txt_list.extend ([i for i in range (655090)]) # lowercase letter txt_list.extend ([i for i in range (97123)]) # numeric txt_list.extend ([i for i in range (48PM57)]) return chr (txt_ list.randint (0) Len (txt_list)-1)]) def rndbgcolor (): # background color return (random.randint (64255), random.randint (64255), random.randint (64255)) def rndtxtcolor2 (): # font color return (random.randint (32127), random.randint (32127), random.randint (32127)) def code (): weight = 240hight = 60 image = Image.new ('RGB', (weight,hight)) (255255255) font = ImageFont.truetype ('msyh.ttc',36) draw = ImageDraw.Draw (image) # fill background color for x in range (weight): for y in range (hight): draw.point ((xQuery) Fill=rndbgcolor () # generate random verification code for T in range (4): rndtxt_2 = rndtxt () print (rndtxt_2) # print the value of the verification code draw.text ((60 * T + 10), rndtxt_2,font=font,fill=rndtxtcolor2 ()) image.show () code () so far I believe that "how to use Python to achieve random generation of picture verification code" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Verify generate picture color background letter font content uppercase uppercase lowercase number learning practical deeper code interest practicality actual form Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MySQL vpn Linux macOS