Skip to main content
==================
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)

Metadata

Release files for generate_captcha 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for generate_captcha 0.0.4
File Size Uploaded Provenance Actions
generate_captcha-0.0.4.tar.gz 9.2 MB Without provenance Details for generate_captcha-0.0.4.tar.gz

Release files / generate_captcha-0.0.4.tar.gz

Download URL generate_captcha-0.0.4.tar.gz
Size 9.2 MB
Tags Source
SHA-256 checksum
How to use checksums
6c01a0460c8acbdd268acba11eb2d3239f65f86cebb6d55effcf93ac5e312018
BLAKE2b-256 checksum
How to use checksums
364867112ba6b4d6b2182e7ec5c88c6cdc182432940a9b6788284d2bc77909e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for generate_captcha 0.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page