Generate verification code based on Pil library.
Project description
==================
Generate Captcha
==================
基于第三方库 ``Pillow`` 生成验证码图片
---------
创建画笔
---------
.. 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)
Generate Captcha
==================
基于第三方库 ``Pillow`` 生成验证码图片
---------
创建画笔
---------
.. 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)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
generate_captcha-0.0.2.tar.gz
(9.2 MB
view hashes)