本文整理汇总了Python中captcha.image.ImageCaptcha类的典型用法代码示例。如果您正苦于以下问题:Python ImageCaptcha类的具体用法?Python ImageCaptcha怎么用?Python ImageCaptcha使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ImageCaptcha类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getCaptcha
def getCaptcha(self):
_chars = 'ABCDEFGHJKLMNPQRSTUVWXY23456789'
chars = random.sample(_chars, 4)
self.session.captcha = (''.join(chars)).lower()
image = ImageCaptcha(fonts=['./static/fonts/arial.ttf', './static/fonts/arial.ttf'])
data = image.generate(chars)
return data
示例2: on_get
def on_get(self, req, resp):
# Generate a new captcha
image = ImageCaptcha(fonts = ['fonts/UbuntuMono-R.ttf'])
captcha_uuid = str(uuid.uuid4())
captcha_answer = ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for _ in range(6))
captcha_image = 'data:image/png;base64,' + str(base64.b64encode(image.generate(captcha_answer).getvalue())).split("'")[1]
# The answer should be case insensitive, the caps are just there for the bots
captcha_answer = captcha_answer.lower()
captcha = db.Captcha(
uuid = captcha_uuid,
answer_hash = nacl.hash.sha256(str.encode(captcha_uuid + captcha_answer), encoder = nacl.encoding.RawEncoder),
ip_address_hash = nacl.hash.sha256(str.encode(captcha_uuid + req.remote_addr), encoder = nacl.encoding.RawEncoder),
user_agent_hash = nacl.hash.sha256(str.encode(captcha_uuid + req.user_agent), encoder = nacl.encoding.RawEncoder)
)
db_session = req.context['db_session']
db_session.add(captcha)
db_session.commit()
resp.status = falcon.HTTP_200
resp.body = json.dumps({
'uuid': captcha_uuid,
'image': captcha_image
})
示例3: do_image
def do_image():
# image = ImageCaptcha(fonts=['./comic.ttf', './comicbd.ttf'])
image = ImageCaptcha(width=100, height=50)
# data = image.generate('1234')
# assert isinstance(data, BytesIO)
image.write('1234', 'out.%s' % fmt_suffix.get(fmt, fmt), fmt=fmt)
示例4: do_image
def do_image():
# image = ImageCaptcha(fonts=['./comic.ttf', './comicbd.ttf'])
image = ImageCaptcha()
# data = image.generate('1234')
# assert isinstance(data, BytesIO)
image.write("1234", "out.jpeg", format="jpeg")
示例5: stress_image
def stress_image(i):
image = ImageCaptcha(width=100, height=50, fonts=['./Mirvoshar.ttf'], font_sizes=[50, 54, 57, 64])
d = str(i)
if os.path.exists(d):
shutil.rmtree(d)
os.makedirs(d)
for t in texts:
image.write(t, os.path.join(d, '%s.%s' % (t, fmt_suffix.get(fmt, fmt))), fmt=fmt)
示例6: test_image_generate
def test_image_generate():
captcha = ImageCaptcha()
data = captcha.generate('1234')
assert hasattr(data, 'read')
captcha = WheezyCaptcha()
data = captcha.generate('1234')
assert hasattr(data, 'read')
示例7: gen_captcha_base64
def gen_captcha_base64(length=None):
length = length if length else LENGTH
image = ImageCaptcha(width=130, height=60) # fonts=FONTS)
text = random_text(length).lower()
im = image.generate(text)
code_signature = sign(text)
b64 = base64.b64encode(im.getvalue()).decode("utf-8")
return f"data:image/png;base64, {b64}", code_signature
示例8: gen_captcha
def gen_captcha(length=None):
length = length if length else LENGTH
image = ImageCaptcha(width=130, height=60) # fonts=FONTS)
text = random_text(length)
image.generate(text)
code_signature = sign(text)
fpath = os.path.join(OUTDIR, code_signature + ".png")
image.write(text, fpath)
return fpath, code_signature
示例9: gen_captcha_text_and_image
def gen_captcha_text_and_image():
image = ImageCaptcha()
#获得随机生成的验证码
captcha_text = random_captcha_text()
#把验证码列表转为字符串
captcha_text = ''.join(captcha_text)
#生成验证码
captcha = image.generate(captcha_text)
image.write(captcha_text, 'captcha/images/' + captcha_text + '.jpg') # 写到文件
示例10: gen_captcha_image_random
def gen_captcha_image_random():
image = ImageCaptcha()
captcha_text = random_captcha_text()
captcha_text = ''.join(captcha_text)
captcha = image.generate(captcha_text)
captcha_image = Image.open(captcha)
captcha_image = np.array(captcha_image)
print(captcha_image)
示例11: gen_captcha_text_and_image
def gen_captcha_text_and_image():
image = ImageCaptcha()
captcha_text = random_captcha_text()
captcha_text = ''.join(captcha_text)
captcha = image.generate(captcha_text)
#image.write(captcha_text, captcha_text + '.jpg') # 写到文件
captcha_image = Image.open(captcha)
captcha_image = np.array(captcha_image)
return captcha_text, captcha_image
示例12: gen
def gen(batch_size=32):
X = np.zeros((batch_size, height, width, 3), dtype=np.uint8)
y = [np.zeros((batch_size, n_class), dtype=np.uint8) for i in range(n_len)]
generator = ImageCaptcha(width=width, height=height)
while True:
for i in range(batch_size):
random_str = ''.join([random.choice(characters) for j in range(4)])
X[i] = generator.generate_image(random_str)
for j, ch in enumerate(random_str):
y[j][i, :] = 0
y[j][i, characters.find(ch)] = 1
yield X, y
示例13: gen_captcha_text_and_image
def gen_captcha_text_and_image():
image = ImageCaptcha()
# captcha_text = random_captcha_text()#使用带有字母和数字的随机组合
captcha_text = random_captcha_num()#使用只有数字的随机组合
captcha_text = ''.join(captcha_text)
captcha = image.generate(captcha_text)
# image.write(captcha_text, save_file+captcha_text + '.jpg') # 写到文件
captcha_image = Image.open(captcha)
captcha_image = np.array(captcha_image)
return captcha_text, captcha_image
示例14: generate_verification_code
def generate_verification_code(request):
'''
'''
code = random_code_generator(5)
image = ImageCaptcha()
image.write(code, 'captcha/{0}.png'.format(code))
verification_code = { 'verification_image': '{0}.png'.format(code),
'code': code}
return HttpResponse(json.dumps(verification_code),
status=200,
content_type='application/json')
示例15: appear
def appear(self):
# Make captcha
row = str(uuid.uuid4())[:4]
img = ImageCaptcha()
data = img.generate("1")
assert isinstance(data, BytesIO)
img.write(row, "captcha.png")
pixmap = QtGui.QPixmap("captcha.png")
self.ui.captcha_lb.setPixmap(pixmap)
os.remove("captcha.png")
# Appear win
self.show()
self.animation_appear.start()