Captcha Generator
The `captcha` library (current version 0.7.1) is a Python utility designed to generate both image and audio CAPTCHAs. It provides a simple API to create various forms of captchas, useful for preventing bots in web applications. The library is actively maintained with regular releases addressing features, bug fixes, and security enhancements.
Common errors
-
ModuleNotFoundError: No module named 'captcha.image'
cause This typically occurs if you're using an outdated import path, trying to directly import `ImageCaptcha` or `AudioCaptcha` from the top-level `captcha` package after v0.5.0, or if the module name is misspelled.fixChange your import statement to `from captcha.image import ImageCaptcha` (or `from captcha.audio import AudioCaptcha`). Ensure `captcha` is correctly installed. -
ModuleNotFoundError: No module named 'PIL'
cause The `captcha` library relies on `Pillow` (a fork of PIL) for image manipulation. This error indicates `Pillow` is not installed.fixInstall `Pillow` using `pip install Pillow`. If you have a legacy `PIL` installation, uninstall it and install `Pillow`. -
OSError: cannot open resource
cause This error often occurs when `ImageCaptcha` cannot find the default font file (`arial.ttf`). This can happen on systems without the expected font path or if the specified font is missing.fixSpecify a custom font path that exists on your system during `ImageCaptcha` initialization, e.g., `ImageCaptcha(fonts=['/path/to/your/custom_font.ttf'])`. Common font locations include `/usr/share/fonts/truetype` on Linux, `C:\Windows\Fonts` on Windows, or `/System/Library/Fonts` on macOS.
Warnings
- breaking The library underwent a significant code restructuring in version 0.5.0, which changed import paths for `ImageCaptcha` and `AudioCaptcha`.
- gotcha Versions prior to 0.7.1 used `secrets.randint` which was later replaced with more secure alternatives. While `secrets.randint` is generally secure, specific edge cases or platform behaviors might have been improved.
- gotcha Compatibility with the `Pillow` library is crucial. `captcha` v0.7.0 included updates specifically for Pillow compatibility, indicating potential issues with older `Pillow` versions.
Install
-
pip install captcha
Imports
- ImageCaptcha
from captcha import ImageCaptcha
from captcha.image import ImageCaptcha
- AudioCaptcha
from captcha.audio import AudioCaptcha
Quickstart
import os
from captcha.image import ImageCaptcha
# Generate an image captcha
image = ImageCaptcha(width=280, height=90, fonts=['/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf'])
# Fallback for systems where default font path might not exist
# For Windows: C:\Windows\Fonts\arial.ttf
# For macOS: /System/Library/Fonts/Arial.ttf
# For Linux: Check common paths like /usr/share/fonts/truetype
data = image.generate('1234')
output_filename = 'captcha_image.png'
image.write('1234', output_filename)
print(f"Generated captcha image: {output_filename}")
# Generate an audio captcha (requires 'pydub' and 'ffmpeg' or 'libav' for advanced audio formats)
# from captcha.audio import AudioCaptcha
# audio = AudioCaptcha()
# audio_filename = 'captcha_audio.wav'
# audio.write('1234', audio_filename)
# print(f"Generated captcha audio: {audio_filename}")