In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
 
            
                     
                
2025-10-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about

The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r


A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from

Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





 
             
            About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.