Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
Hiboo
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
frju365
Hiboo
Commits
77983842
Commit
77983842
authored
5 years ago
by
kaiyou
Browse files
Options
Downloads
Patches
Plain Diff
Add a sample image captcha
parent
ae838ad8
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
hiboo/captcha/captcha.py
+58
-0
58 additions, 0 deletions
hiboo/captcha/captcha.py
hiboo/captcha/forms.py
+1
-1
1 addition, 1 deletion
hiboo/captcha/forms.py
with
59 additions
and
1 deletion
hiboo/captcha/captcha.py
+
58
−
0
View file @
77983842
...
...
@@ -2,6 +2,10 @@ from wtforms import widgets
from
hiboo.captcha
import
fields
import
random
import
string
import
io
import
base64
from
PIL
import
Image
,
ImageDraw
,
ImageFont
class
DummyCaptchaField
(
fields
.
CaptchaField
):
...
...
@@ -35,3 +39,57 @@ class DummyCaptchaField(fields.CaptchaField):
field
.
data
.
isdigit
()
and
challenge
==
int
(
field
.
data
)
)
class
GeneratedTextImageCaptchaField
(
fields
.
CaptchaField
):
"""
Generate a random text and build a fuzzy image based on that text.
This is the most common historical captcha type, although pretty easily
defeated by OCR nowadays.
"""
widget
=
widgets
.
TextInput
()
charset
=
string
.
ascii_uppercase
+
string
.
digits
def
_value
(
self
):
return
""
def
challenge
(
self
):
self
.
label
=
"
Please copy the following text:
"
length
=
random
.
randint
(
5
,
8
)
return
""
.
join
(
random
.
choice
(
self
.
charset
)
for
_
in
range
(
length
))
def
check
(
self
,
challenge
,
form
,
field
):
return
challenge
.
lower
()
==
field
.
data
.
lower
()
def
make_image
(
self
,
text
):
font
=
ImageFont
.
truetype
(
"
captcha.ttf
"
,
14
)
size
=
font
.
getsize
(
text
)
size
=
(
size
[
0
]
*
2
,
int
(
size
[
1
]
*
1.4
))
image
=
Image
.
new
(
"
RGB
"
,
size
,
"
#ffffff
"
)
xpos
=
2
for
char
in
text
:
drawn
=
"
{}
"
.
format
(
char
)
fgimage
=
Image
.
new
(
"
RGB
"
,
size
,
"
#000000
"
)
charimage
=
Image
.
new
(
"
L
"
,
font
.
getsize
(
drawn
),
"
#000000
"
)
chardraw
=
ImageDraw
.
Draw
(
charimage
)
chardraw
.
text
((
0
,
0
),
drawn
,
font
=
font
,
fill
=
"
#ffffff
"
)
charimage
=
charimage
.
rotate
(
random
.
randrange
(
-
15
,
15
),
expand
=
0
,
resample
=
Image
.
BICUBIC
)
charimage
=
charimage
.
crop
(
charimage
.
getbbox
())
maskimage
=
Image
.
new
(
"
L
"
,
size
)
maskimage
.
paste
(
charimage
,
(
xpos
,
4
,
xpos
+
charimage
.
size
[
0
],
4
+
charimage
.
size
[
1
]))
size
=
maskimage
.
size
image
=
Image
.
composite
(
fgimage
,
image
,
maskimage
)
xpos
=
xpos
+
2
+
charimage
.
size
[
0
]
image
=
image
.
crop
((
0
,
0
,
xpos
+
1
,
size
[
1
]))
return
image
def
__call__
(
self
,
**
kwargs
):
image
=
self
.
make_image
(
self
.
context
[
"
challenge
"
])
image_png
=
io
.
BytesIO
()
image
.
save
(
image_png
,
"
PNG
"
)
image_string
=
base64
.
b64encode
(
image_png
.
getvalue
()).
decode
(
"
ascii
"
)
return
widgets
.
HTMLString
(
'
<img src=
"
data:image/png;base64,{}
"
>
'
.
format
(
image_string
)
+
super
(
GeneratedTextImageCaptchaField
,
self
).
__call__
(
**
kwargs
)
)
This diff is collapsed.
Click to expand it.
hiboo/captcha/forms.py
+
1
−
1
View file @
77983842
...
...
@@ -6,5 +6,5 @@ import flask_wtf
class
DisplayForm
(
flask_wtf
.
FlaskForm
):
test
=
fields
.
StringField
()
captcha
=
captcha
.
Dummy
CaptchaField
()
captcha
=
captcha
.
GeneratedTextImage
CaptchaField
()
submit
=
fields
.
SubmitField
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment