https://github.com/youngershen/easy-captcha
a very easy to use captcha image generator.
https://github.com/youngershen/easy-captcha
captcha django flask web
Last synced: over 1 year ago
JSON representation
a very easy to use captcha image generator.
- Host: GitHub
- URL: https://github.com/youngershen/easy-captcha
- Owner: youngershen
- License: mit
- Created: 2018-07-30T10:17:08.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-09-02T06:54:29.000Z (almost 6 years ago)
- Last Synced: 2025-03-20T07:06:55.339Z (over 1 year ago)
- Topics: captcha, django, flask, web
- Language: Python
- Size: 17.7 MB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Easy Captcha







------
## What is it
this is a very easy to use python package that helps you to generate the image captchas, in the help of this package you
can easily make your own captcha design rather than the default design.
## Python supported
* CPYTHON 3.6
* CPYTHON 3.7
## Installation
* pip install easy-captcha
* python setup.py install
## Quick start
```python
from captcha.generator import DefaultGenerator
generator = DefaultGenerator()
captcha = generator.make_captcha(string='ABCD')
captcha.save('test-default.png')
```
the make_captcha method return a PIL Image object, you
can save it with your any type you wanted, easy to use.
## Samples
from captcha import DefaultGenerator

from captcha import SimpleGenerator

from captcha import SimpleChineseGenerator

## Advance topic
### Custom font
```python
from pathlib import Path
from captcha.generator import DefaultGenerator
class MyCaptchaGenerator(DefaultGenerator):
def _get_font(self, size):
p = Path('./fonts/test.otf')
font = self._load_font(path=p, size=size)
return font
```
if you just want to use your font instead of the default font,
just reimplement the _get_font method, make the method return
your font.
### Custom captcha generator
if you want to custom the captcha generator, you just need to subclass the **captcha.generator.BaseGenerator** class. when
you subclass the **BaseGenerator** then you could use the method
inside the **BaseGenerator** to make your own style captcha genrator.
if the default drawing methods are not well enough for you, you
could just use the **pillow** staff to make your own generator.
this whole package is based on **pillow**, so just feel free to
modify it.
for more details just check the code below. the important thing
is that when you subclass the **BaseGenerator**, you just need
to implement the **make_captcha** method and **_get_font** method.
```python
class DefaultGenerator(BaseGenerator):
FONT = 'TruenoBdOlIt.otf'
def make_captcha(self,
string: str = None,
font_size: int = 48,
image_size: tuple = None):
captcha = self._make_captcha(string, font_size)
size = image_size if image_size else self.size
captcha = self._resize(captcha, size)
return captcha
def _make_captcha(self, string, font_size):
font = self._get_font(font_size)
char_images = self._make_char_images(string,
font,
rotate=None,
color=self._rand_color)
image = self._composite_char_images(char_images,
color=self._get_color(255,
255,
255))
self._rand_noise_lines(image, number=3)
return image
def _get_font(self, size: int):
font = self._load_font(name=self.FONT, size=size)
return font
```
## Future support feature
- [x] Basic png captcha support.
- [ ] GIF format support.
- [ ] Audio format support.
- [x] Django framework intergration, see [this](https://github.com/youngershen/django-easy-captcha).
- [ ] Flask framework intergration.