-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaptcha.py
50 lines (40 loc) · 1.22 KB
/
captcha.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
import random
import string
from PIL import Image, ImageDraw, ImageFont
def captcha_auth():
# using random.choices()
# generating random strings
code = "".join(random.choices(string.ascii_uppercase + string.digits, k=7))
# create a new image
image = Image.new("RGB", (125, 45), (255, 255, 255))
# draw a random string of characters on the image
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("arial.ttf", 20)
draw.text((10, 10), code, font=font, fill=(0, 0, 0))
# save the image to a file
image.save("captcha.png")
# display the CAPTCHA to the user
captcha = Image.open("captcha.png")
captcha.show()
# ask the user to enter the text that they see
text = input("Enter the text in the image or regenerate/ exit: ").upper()
# check if the user entered the correct text
# human verified
if text == code:
human = True
# regenerate captcha
elif text == "REGENERATE":
human = captcha_auth()
# exit
elif text == "EXIT":
exit()
# human not verified
else:
human = False
# close image
captcha.close()
# remove image
os.remove("captcha.png")
# return if human or not
return human