-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds graphical captcha prototype (#124)
* adds kinda graphical captcha * black * Update Pipfile * Don't require 3.8. Co-authored-by: infra <infra@sterile.solutions>
- Loading branch information
Showing
6 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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" |
This file contains 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
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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.