Generate verification code based on Pil library.
Project 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)
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)
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
File details
Details for the file generate_captcha-0.0.4.tar.gz.
File metadata
- Download URL: generate_captcha-0.0.4.tar.gz
- Upload date:
- Size: 9.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c01a0460c8acbdd268acba11eb2d3239f65f86cebb6d55effcf93ac5e312018
|
|
| MD5 |
ddafef82e76e60f63121ddecd1a0eda0
|
|
| BLAKE2b-256 |
364867112ba6b4d6b2182e7ec5c88c6cdc182432940a9b6788284d2bc77909e1
|