Package: bettercaptcha
CaptchaCaptcha illustrator with Pillow and Numpy for Python 3.6 or higherInstallationInstall without distortion setting:pip install captcha-MagicaFreakInstall with distortion setting:pip install bettercaptcha[distortion]orpip install bettercaptcha
pip install numpyCaptcha ClassAll the possile CAPTCHA variantsCaptchaImageThe classCaptchaImagegenerates an Image with given Text or generated Text. The look of the CAPTCHA can be changed
through thedefault Arguments,setting Argumentsand diffrentoverwrites.The most basic usage of this class looks like this:frombettercaptchaimportCaptchaImagecaptcha=CaptchaImage()image=captcha.create()text=captcha.textHere is theimageaPIL.Imageinstance andtextthe text what is written in the CAPTCHADefault Arguments:NameTypeDescriptionDefaultfontstrpath to the font filearial.ttffont_sizeintsize of the font24colortupletext color of the CAPTCHA(0, 0, 0) => blacktextstrtext on the captcharandom generated stringchar_amountintlength of the random generated string12Setting Arguments:NameTypeDescriptionDefaultbackgroundBackgroundsets a background on the captcha (see more underBackground)Nonedistortionbooldistorts the text (Needs the numpy package)Falselinesintadds the amount of lines between random PointsNonepointsintadds the amount of points on random PointsNoneframebooladds a frame around the CAPTCHAFalseBackgroundThe classBackgroundhas all modes for the settingbackground.The first mode isBackground.RANDOM. It generates a random color in every Pixel of the background. The color is
choosen from the listCaptchaImage.colorsthat can beoverwritten.The second mode isBackground.COLOR. It colors the background with the text color with only 50% alpha.OverwritesOverwrites can be used in many diffrent cases, but the 2 biggest would be if the captcha is generated with some
flexibiltiy, or you generate another captcha with diffrent look but same text.The default argumnets can't be overwritten except color The overwrites for color would look like this:captcha=CaptchaImage()captcha.color=(52,52,52)Important to notice thatcaptcha.colorcan only be a rgb value!All the setting arguments can be overwritten, this would look like this:captcha=CaptchaImage()captcha.settings['distortion']=Truecaptcha.settings['background']=Background.RANDOMThe third kind of overwrites are things that are normaly contstaned. Under this countsCaptchaImage.colors,CaptchaImage.distortionandCaptchaImage.distortion_factor.CaptchaImage.colorsis a list or RGB colors for the background settingBackground.RANDOM, it can be overwritten with
another list of RGB colors, like so:captcha=CaptchaImage(background=Background.RANDOM)captcha.colors=[(255,0,0),(0,255,0),(0,0,255)]CaptchaImage.distortionis a mathematical function. To overwrite it you need to create another function with the
valuesx,aandw. It can be overwritten like this:frommathimportsin,picaptcha=CaptchaImage(distrotion=True)defdistortion(x,a,w):returna*sin(2*pi*x*w)# ordistortion=lambdax,a,w:a*sin(2*pi*x*w)captcha.distortion=distortionCaptchaImage.distortion_factoris the last overwrite it is a tuple of ints that are influencing the valuesaandw. The calculation ofaandwlooks like this:a=image_height/distortion_factor[1]w=distortion_factor[0]/image_widthTo overwrite the complete distortion factor you would do this:captcha=CaptchaImage(distrotion=True)captcha.distortion_factor=(0.59,17.0)CaptchaImage.rand_chars(amount, chars)This function generates a random string with the length of the amount, but it only usese every character one time out of
the list. Important to notice that if you give a chars list smaller than the amount it will not work!text=CaptchaImage.rand_chars(12)Arguments:NameTypeDescriptionDefaultamountintlength of the string12charslistlist of used charactersall ascii letters and digits