docs: update audio and image docs

This commit is contained in:
Hsiaoming Yang
2023-07-29 12:19:28 +09:00
parent 8c306694e5
commit 2d53d07d3d
2 changed files with 69 additions and 7 deletions

View File

@@ -22,14 +22,14 @@ Generating audio CAPTCHA with the :class:`AudioCaptcha`` class is remarkably sim
captcha = AudioCaptcha()
data: bytearray = captcha.generate('1234')
.. note::
The default voice library only includes numbers from 0 to 9.
Voice library
-------------
We can build our customized voice library with the help of ``espeak`` and ``ffmpeg``:
The ``AudioCaptcha`` module comes with built-in voice files for
numbers from 0 to 9. However, for enhanced security and customization,
it is highly recommended to use your own voice library. This section
will guide you on how to generate your own voice library using ``espeak``
and ``ffmpeg``.
.. code-block:: bash
@@ -47,5 +47,38 @@ We can build our customized voice library with the help of ``espeak`` and ``ffmp
rm "$ESLANG/$i/orig_default.wav"
done
Then use the voice library:
.. code-block:: python
from captcha.audio import AudioCaptcha
voice_dir = "path/to/en" # we generated the wav files in "en" folder
captcha = AudioCaptcha(voice_dir)
Web server
----------
In addition to generating and saving the voice files in a directory or
cloud storage like Amazon S3, you can also serve the Voice CAPTCHA audio
files on-the-fly.
Let's explore how to use the CAPTCHA library to dynamically serve audio
CAPTCHAs within a Flask application.
.. code-block:: python
from io import BytesIO
from flask import Flask, Response
from captcha.audio import AudioCaptcha
audio = AudioCaptcha()
app = Flask(__name__)
@app.route("/captcha")
def captcha_view():
# add your own logic to generate the code
code = "1234"
data = audio.generate(code)
return Response(BytesIO(data), mimetype="audio/wav")

View File

@@ -7,6 +7,8 @@ Image Captcha
----
This module is compatibile with Pillow 9.4.0+.
.. module:: captcha.image
:noindex:
@@ -33,7 +35,34 @@ The result image would be something like:
Fonts
-----
You should use
The ``ImageCaptcha`` library comes with one built-in font named "DroidSansMono",
which is licensed under the Apache License 2.0. However, it is recommended to use
your own custom fonts for generating CAPTCHA images.
Web server
.. code-block:: python
custom_fonts = ['path/to/your/custom_font.ttf']
captcha = ImageCaptcha(fonts=[custom_fonts])
On the fly
----------
Let's explore how to use the CAPTCHA library to dynamically render image
CAPTCHAs within a Flask application. This allows you to generate CAPTCHA
images dynamically and serve them directly to users when they access a
specific endpoint.
.. code-block:: python
from flask import Flask, Response
from captcha.image import ImageCaptcha
image = ImageCaptcha()
app = Flask(__name__)
@app.route("/captcha")
def captcha_view():
# add your own logic to generate the code
code = "ABCD"
data = image.generate(code)
return Response(data, mimetype="image/png")