Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to randomly generate Picture Verification Code

2025-02-24 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report