Skip to content

Commit

Permalink
docs: update audio and image docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lepture committed Jul 29, 2023
1 parent 8c30669 commit 2d53d07
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
43 changes: 38 additions & 5 deletions docs/audio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
33 changes: 31 additions & 2 deletions docs/image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Image Captcha

----

This module is compatibile with Pillow 9.4.0+.

.. module:: captcha.image
:noindex:

Expand All @@ -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.

.. code-block:: python
custom_fonts = ['path/to/your/custom_font.ttf']
captcha = ImageCaptcha(fonts=[custom_fonts])
Web server
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")

0 comments on commit 2d53d07

Please sign in to comment.