from random
import randint
import PIL
from PIL
import Image
, ImageDraw
, ImageFont
class VerifyCode():
def __init__(self
,width
=100,height
=40,size
=4):
self
.width
= width
self
.height
= height
self
.size
= size
def draw(self
):
im
= Image
.new
('RGB',(self
.width
, self
.height
),self
.__randcolor
(150,250))
self
.pen
= ImageDraw
.Draw
(im
)
self
.code
= self
.__rand_string
()
self
.__draw_string
()
self
.__draw_points
()
self
.__draw_line
()
im
.save
('yzm.png')
def __draw_points(self
):
for i
in range(200):
self
.pen
.point
((randint
(1,self
.width
-1),randint
(1,self
.height
-1)),self
.__randcolor
(10,80))
def __randcolor(self
,low
,high
):
return randint
(low
,high
),randint
(low
,high
),randint
(low
,high
)
def __rand_string(self
):
res
= ''
for i
in range(self
.size
):
res
+= str(randint
(0,9))
return res
def __draw_string(self
):
font1
= ImageFont
.truetype
('SIMLI.TTF', size
= 20, encoding
='utf-8')
for i
in range(self
.size
):
x
= 10 + i
*((self
.width
- 10) // self
.size
)
y
= randint
(5,15)
self
.pen
.text
((x
, y
),self
.code
[i
],font
= font1
,fill
=self
.__randcolor
(50,100))
def __draw_line(self
):
for i
in range(5):
x1
= randint
(1,self
.width
-1)
x2
= randint
(1,self
.width
-1)
y1
= randint
(1,self
.height
-1)
y2
= randint
(1,self
.height
-1)
self
.pen
.line
([(x1
,y1
),(x2
,y2
)], width
=1,fill
=self
.__randcolor
(30,100))
if __name__
== '__main__':
vc
=VerifyCode
()
vc
.draw
()
转载请注明原文地址: https://mac.8miu.com/read-493749.html