Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bettercaptcha

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bettercaptcha

Captcha illustrator with Pillow and Numpy for Python 3.6 or higher

  • 1.0.2
  • PyPI
  • Socket score

Maintainers
1

Captcha

Captcha illustrator with Pillow and Numpy for Python 3.6 or higher

Installation

Install without distortion setting:

pip install captcha-MagicaFreak

Install with distortion setting:

pip install bettercaptcha[distortion]

or

pip install bettercaptcha
pip install numpy

Captcha Class

All the possile CAPTCHA variants

CaptchaImage

The class CaptchaImage generates an Image with given Text or generated Text. The look of the CAPTCHA can be changed through the default Arguments, setting Arguments and diffrent overwrites.

The most basic usage of this class looks like this:

from bettercaptcha import CaptchaImage

captcha = CaptchaImage()

image = captcha.create()
text = captcha.text

Here is the image a PIL.Image instance and text the text what is written in the CAPTCHA

Default Arguments:
NameTypeDescriptionDefault
fontstrpath to the font filearial.ttf
font_sizeintsize of the font24
colortupletext color of the CAPTCHA(0, 0, 0) => black
textstrtext on the captcharandom generated string
char_amountintlength of the random generated string12
Setting Arguments:
NameTypeDescriptionDefault
backgroundBackgroundsets a background on the captcha (see more under Background)None
distortionbooldistorts the text (Needs the numpy package)False
linesintadds the amount of lines between random PointsNone
pointsintadds the amount of points on random PointsNone
framebooladds a frame around the CAPTCHAFalse
Background

The class Background has all modes for the setting background.

The first mode is Background.RANDOM. It generates a random color in every Pixel of the background. The color is choosen from the list CaptchaImage.colors that can be overwritten.

The second mode is Background.COLOR. It colors the background with the text color with only 50% alpha.

Overwrites

Overwrites 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 that captcha.color can only be a rgb value!

All the setting arguments can be overwritten, this would look like this:

captcha = CaptchaImage()
captcha.settings['distortion'] = True
captcha.settings['background'] = Background.RANDOM

The third kind of overwrites are things that are normaly contstaned. Under this counts CaptchaImage.colors, CaptchaImage.distortion and CaptchaImage.distortion_factor.

CaptchaImage.colors is a list or RGB colors for the background setting Background.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.distortion is a mathematical function. To overwrite it you need to create another function with the values x, a and w. It can be overwritten like this:

from math import sin, pi

captcha = CaptchaImage(distrotion=True)


def distortion(x, a, w):
    return a * sin(2 * pi * x * w)
# or
distortion = lambda x, a, w: a * sin(2 * pi * x * w)

captcha.distortion = distortion

CaptchaImage.distortion_factor is the last overwrite it is a tuple of ints that are influencing the values a and w . The calculation of a and w looks like this:

a = image_height / distortion_factor[1]
w = distortion_factor[0] / image_width

To 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:

NameTypeDescriptionDefault
amountintlength of the string12
charslistlist of used charactersall ascii letters and digits

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc