Skip to content

Instantly share code, notes, and snippets.

@loRes228
Created January 26, 2025 15:00
Show Gist options
  • Save loRes228/27b8df9ea84ed08fd98fe0a664f5f9d3 to your computer and use it in GitHub Desktop.
Save loRes228/27b8df9ea84ed08fd98fe0a664f5f9d3 to your computer and use it in GitHub Desktop.
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