Metadata-Version: 1.0
Name: generate_captcha
Version: 0.0.4
Summary: Generate verification code based on Pil library.
Home-page: https://github.com/seekplum/generate_captcha
Author: seekplum
Author-email: 1131909224m@sina.cn
License: GPL-3.0
Description-Content-Type: UNKNOWN
Description: ==================
        Generate Captcha
        ==================
        
        * 基于第三方库 ``Pillow`` 生成验证码图片
        * 注意： 若不指定字体类型，需要把文件中的 ``msyh.ttf`` 拷贝到系统字体库中
        
        
        ---------
        创建画笔
        ---------
        
        .. code-block:: python
        
            draw = ImageDraw.Draw(img)  # 创建画笔
        
        ----------
        增加干扰线
        ----------
        
        .. code-block:: python
        
            def create_lines():
                """绘制干扰线
                """
                line_num = random.randint(*n_line)  # 干扰线条数
        
                for i in range(line_num):
                    # 起始点
                    begin = (random.randint(0, size[0]), random.randint(0, size[1]))
                    # 结束点
                    end = (random.randint(0, size[0]), random.randint(0, size[1]))
                    draw.line([begin, end], fill=(0, 0, 0))
        ----------
        增加干扰点
        ----------
        
        .. code-block:: python
        
            def create_points():
                """绘制干扰点
                """
                chance = min(100, max(0, int(point_chance)))  # 大小限制在[0, 100]
        
                for w in range(width):
                    for h in range(height):
                        tmp = random.randint(0, 100)
                        if tmp > 100 - chance:
                            draw.point((w, h), fill=(0, 0, 0))
        
        -----------------
        绘制验证码字符
        -----------------
        
        .. code-block:: python
        
            def create_text(xy_, font_type_):
                """绘制验证码字符
                """
                c_chars = get_chars()
                text_ = '%s' % ' '.join(c_chars)  # 每个字符前后以空格隔开
                if font_type_ is None:
                    curr_path = os.path.dirname(os.path.abspath(__file__))
                    font_type_ = os.path.join(os.path.dirname(curr_path), "packages", "msyh.ttf")
                font = ImageFont.truetype(font_type_, font_size)
                if xy_ is None:
                    font_width, font_height = font.getsize(text_)
                    xy_ = ((width - font_width) / 3, (height - font_height) / 3)  # 左右距离/上下距离
                draw.text(xy_, text_, font=font, fill=fg_color)
        
                return ''.join(c_chars)
        
        ---------
        保存图片
        ---------
        
        .. code-block:: python
        
            # 图片路径
            file_name_ = file_name if file_name else text
            file_name = "{}.{}".format(file_name_, suffix)
            file_path = os.path.join(image_path, file_name)
            img.save(file_path)
Keywords: Pil,Captcha,Image
Platform: any
