Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Captcha illustrator with Pillow and Numpy for Python 3.6 or higher
Install without distortion setting:
pip install captcha-MagicaFreak
Install with distortion setting:
pip install bettercaptcha[distortion]
or
pip install bettercaptcha
pip install numpy
All the possile CAPTCHA variants
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
Name | Type | Description | Default |
---|---|---|---|
font | str | path to the font file | arial.ttf |
font_size | int | size of the font | 24 |
color | tuple | text color of the CAPTCHA | (0, 0, 0) => black |
text | str | text on the captcha | random generated string |
char_amount | int | length of the random generated string | 12 |
Name | Type | Description | Default |
---|---|---|---|
background | Background | sets a background on the captcha (see more under Background) | None |
distortion | bool | distorts the text (Needs the numpy package) | False |
lines | int | adds the amount of lines between random Points | None |
points | int | adds the amount of points on random Points | None |
frame | bool | adds a frame around the CAPTCHA | False |
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 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)
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:
Name | Type | Description | Default |
---|---|---|---|
amount | int | length of the string | 12 |
chars | list | list of used characters | all ascii letters and digits |
FAQs
Captcha illustrator with Pillow and Numpy for Python 3.6 or higher
We found that bettercaptcha demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.