Skip to content

youngershen/easy-captcha

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Easy Captcha

Travis codecov PyPI - License PyPI PyPI - Wheel PyPI - Python Version GitHub last commit


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

    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

Default

from captcha import SimpleGenerator

Simple

from captcha import SimpleChineseGenerator

Simple

Advance topic

Custom font

    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.

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

  • Basic png captcha support.
  • GIF format support.
  • Audio format support.
  • Django framework intergration, see this.
  • Flask framework intergration.

About

a very easy to use captcha image generator.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages