Created
January 26, 2025 15:00
-
-
Save loRes228/27b8df9ea84ed08fd98fe0a664f5f9d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from asyncio import to_thread | |
from random import sample | |
from string import digits | |
from typing import TYPE_CHECKING | |
from captcha.image import ImageCaptcha | |
if TYPE_CHECKING: | |
from io import BytesIO | |
async def generate_captcha( | |
length: int = 6, | |
chars: str = digits, | |
width: int = 380, | |
height: int = 180, | |
) -> tuple[bytes, str]: | |
captcha_factory = ImageCaptcha(width=width, height=height) | |
code = "".join(sample(population=chars, k=length)) | |
image: BytesIO = await to_thread(captcha_factory.generate, chars=code) | |
return image.getvalue(), code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment