{"id":7066,"library":"captcha","title":"Captcha Generator","description":"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.","status":"active","version":"0.7.1","language":"en","source_language":"en","source_url":"https://github.com/lepture/captcha","tags":["captcha","security","image-generation","audio-generation","anti-bot"],"install":[{"cmd":"pip install captcha","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for image generation functionality; specifically Pillow>=8.2.0 is recommended for v0.7.0+.","package":"Pillow","optional":false}],"imports":[{"note":"Import paths were restructured in v0.5.0; direct imports from `captcha` no longer work for image/audio modules.","wrong":"from captcha import ImageCaptcha","symbol":"ImageCaptcha","correct":"from captcha.image import ImageCaptcha"},{"symbol":"AudioCaptcha","correct":"from captcha.audio import AudioCaptcha"}],"quickstart":{"code":"import os\nfrom captcha.image import ImageCaptcha\n\n# Generate an image captcha\nimage = ImageCaptcha(width=280, height=90, fonts=['/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf'])\n# Fallback for systems where default font path might not exist\n# For Windows: C:\\Windows\\Fonts\\arial.ttf\n# For macOS: /System/Library/Fonts/Arial.ttf\n# For Linux: Check common paths like /usr/share/fonts/truetype\n\ndata = image.generate('1234')\n\noutput_filename = 'captcha_image.png'\nimage.write('1234', output_filename)\nprint(f\"Generated captcha image: {output_filename}\")\n\n# Generate an audio captcha (requires 'pydub' and 'ffmpeg' or 'libav' for advanced audio formats)\n# from captcha.audio import AudioCaptcha\n# audio = AudioCaptcha()\n# audio_filename = 'captcha_audio.wav'\n# audio.write('1234', audio_filename)\n# print(f\"Generated captcha audio: {audio_filename}\")","lang":"python","description":"This quickstart demonstrates how to generate a simple image CAPTCHA. It initializes `ImageCaptcha` and then generates an image for a given text, saving it to a file. Note: Font paths may need adjustment based on your operating system. Audio captcha generation is also possible but requires additional dependencies (e.g., `pydub`, `ffmpeg`)."},"warnings":[{"fix":"Update your import statements from `from captcha import ImageCaptcha` to `from captcha.image import ImageCaptcha` (and similarly for `AudioCaptcha`).","message":"The library underwent a significant code restructuring in version 0.5.0, which changed import paths for `ImageCaptcha` and `AudioCaptcha`.","severity":"breaking","affected_versions":"<0.5.0"},{"fix":"Upgrade to `captcha>=0.7.1` to benefit from the latest security improvements in random number generation.","message":"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.","severity":"gotcha","affected_versions":"<0.7.1"},{"fix":"Ensure you are using a compatible version of `Pillow`. The `captcha` library's `setup.py` generally specifies `Pillow>=8.2.0` for recent versions. Update `pip install --upgrade Pillow` if experiencing issues.","message":"Compatibility with the `Pillow` library is crucial. `captcha` v0.7.0 included updates specifically for Pillow compatibility, indicating potential issues with older `Pillow` versions.","severity":"gotcha","affected_versions":"All versions, especially >=0.7.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Change your import statement to `from captcha.image import ImageCaptcha` (or `from captcha.audio import AudioCaptcha`). Ensure `captcha` is correctly installed.","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.","error":"ModuleNotFoundError: No module named 'captcha.image'"},{"fix":"Install `Pillow` using `pip install Pillow`. If you have a legacy `PIL` installation, uninstall it and install `Pillow`.","cause":"The `captcha` library relies on `Pillow` (a fork of PIL) for image manipulation. This error indicates `Pillow` is not installed.","error":"ModuleNotFoundError: No module named 'PIL'"},{"fix":"Specify 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.","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.","error":"OSError: cannot open resource"}]}