Python utility module (36) captcha

surroundings

  • windows 10 64bit
  • python 3.8
  • captcha 0.4

foreword

captcha is a third-party library used to generate image or audio verification. Captcha technology is very common in web applications. In this article, we will take a look at some of its common uses.

Install

Install using pip , execute the command

 pip install captcha

example

Let’s take a look at an example of a picture verification code

 import argparse from captcha.image import ImageCaptcha if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--text', type=str, default='xugaoxiang', help='text that show in the image') opt = parser.parse_args() # 实例化,指定宽度和高度,如果想更换显示的字体,可以使用参数fonts image = ImageCaptcha(width=300, height=100) captcha_text = opt.text # 生成图片data = image.generate_image(captcha_text) # 保存图片image.write(captcha_text, 'captcha.png')

Execute with parameter --text , if there is no parameter, the default value xugaoxiang will be used

 python test.py --text xgx

Next, let’s take a look at the example of the voice verification code, the core code is very similar

 from captcha.audio import AudioCaptcha if __name__ == '__main__': audio = AudioCaptcha() captcha_text = '1234' audio_data = audio.generate(captcha_text) audio.write(captcha_text, 'out.wav')

After execution, the audio file out.wav will be generated in the current directory, the audio will contain the string 1234 , and of course some noises to disturb you.

https://xugaoxiang.com/wp-content/uploads/2022/08/out.wav

If the voice directory is not specified, only numbers can be generated by default. If characters are to be generated, the code will report an error. At this time, we can create our own voice library and use the two espeak (text-to-speech) and ffmpeg (speech processing) to achieve this. The following is the usage method of the ubuntu system.

The first is to install these 2 tools, use apt install

 sudo apt install espeak ffmpeg

Common characters include numbers from 0 to 9, and 26 letters. It is best to distinguish between upper and lower case. After clarifying this, we execute the command

 # 环境变量export ESLANG=en # 创建目录mkdir $ESLANG # 给每个字符创建一个文件夹,使用espeak 生成对应文本的语音,最后使用ffmpeg 处理一下for i in {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9}; do mkdir $ESLANG/$i; espeak -a 150 -s 100 -p 15 -v$ESLANG $i -w $ESLANG/$i/orig_default.wav; ffmpeg -i $ESLANG/$i/orig_default.wav -ar 8000 -ac 1 -acodec pcm_u8 $ESLANG/$i/$i.wav; rm $ESLANG/$i/orig_default.wav; done for i in {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9}; do mkdir $ESLANG/$i; espeak -a 150 -s 100 -p 15 -v$ESLANG $i -w $ESLANG/$i/orig_default.wav; ffmpeg -i $ESLANG/$i/orig_default.wav -ar 8000 -ac 1 -acodec pcm_u8 $ESLANG/$i/$i.wav; rm $ESLANG/$i/orig_default.wav; done

With our own voice library, we can use it and look at the code directly

 from captcha.audio import AudioCaptcha if __name__ == '__main__': # 使用自己的语音库audio = AudioCaptcha(voicedir="en") captcha_text = 'XGX123' audio_data = audio.generate(captcha_text) audio.write(captcha_text, 'out.wav')

In this way, the voice of the characters can also be generated. It should be noted here that ubuntu is case-sensitive, while windows is insensitive, that is to say, X and x are the same thing. Therefore, in windows , it is necessary to do a unified processing of all uppercase or lowercase, and then generate the corresponding voice.

https://xugaoxiang.com/wp-content/uploads/2022/08/out1.wav

As for the part of converting Chinese characters to speech, espeak installed by apt is not supported by default, and an error message Full dictionary is not installed for 'zh' will appear. The solution is to recompile espeak and add the compile parameter --compile=zh That’s it, interested children’s shoes can try it out for themselves.

For more details, you can refer to the official documentation https://pypi.org/project/captcha/

Topics in Python Practical Modules

More useful python modules, please move

https://xugaoxiang.com/category/python/modules/

This article is reprinted from https://xugaoxiang.com/2022/08/24/python-module-36-captcha/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment