Skip to content

Commit

Permalink
Adds graphical captcha prototype (#124)
Browse files Browse the repository at this point in the history
* adds kinda graphical captcha

* black

* Update Pipfile

* Don't require 3.8.

Co-authored-by: infra <infra@sterile.solutions>
  • Loading branch information
itdaniher and i-infra authored Feb 17, 2022
1 parent 624ca84 commit c7cafc7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
13 changes: 13 additions & 0 deletions captcha/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[packages]
pillow = "*"
num2words = "*"

[dev-packages]

[requires]
python_version = "3.9"
41 changes: 41 additions & 0 deletions captcha/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import random
import tempfile

from typing import Tuple

import num2words

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont


def get_challenge_and_answer() -> Tuple[str, int]:
first = random.randint(0, 30)
second = random.randint(0, 30)
answer = first + second
first = num2words.num2words(first)
second = num2words.num2words(second)
# Open an Image
img = Image.open(f"captcha/captcha{random.randint(1,4)}.webp")

# Call draw Method to add 2D graphics in an image
I1 = ImageDraw.Draw(img)

myFont = ImageFont.truetype("FreeMono.ttf", 65)
# Add Text to an image
I1.text(
(80, 80), f"What's {first}\nplus {second}?", font=myFont, fill=(255, 255, 255)
)

temp_file = tempfile.NamedTemporaryFile(
prefix="rendered", suffix=".webp", delete=False
)

# Save the edited image
img.save(temp_file)
return temp_file.name, answer


if __name__ == "__main__":
print(get_challenge_and_answer())
Binary file added captcha/captcha1.webp
Binary file not shown.
Binary file added captcha/captcha2.webp
Binary file not shown.
Binary file added captcha/captcha3.webp
Binary file not shown.
Binary file added captcha/captcha4.webp
Binary file not shown.

0 comments on commit c7cafc7

Please sign in to comment.