本文整理汇总了Python中captcha.image.ImageCaptcha方法的典型用法代码示例。如果您正苦于以下问题:Python image.ImageCaptcha方法的具体用法?Python image.ImageCaptcha怎么用?Python image.ImageCaptcha使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类captcha.image
的用法示例。
在下文中一共展示了image.ImageCaptcha方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_image_captcha
# 需要导入模块: from captcha import image [as 别名]
# 或者: from captcha.image import ImageCaptcha [as 别名]
def create_image_captcha(captcha_text):
image_captcha = ImageCaptcha()
# Create the captcha image.
image = image_captcha.generate_image(captcha_text)
# Add noise curve for the image.
# image_captcha.create_noise_curve(image, image.getcolors())
# Add noise dots for the image.
# image_captcha.create_noise_dots(image, image.getcolors())
# Save the image to a png file.
image_file = "./captcha_"+captcha_text + ".png"
imgByteArr = BytesIO()
image.save(imgByteArr, format='PNG')
imgByteArr = imgByteArr.getvalue()
open("test.png", "wb").write(imgByteArr)
#image_captcha.write(captcha_text, image_file)
print(image_file + " has been created.")
# Create an audio captcha file.
示例2: gen_captcha
# 需要导入模块: from captcha import image [as 别名]
# 或者: from captcha.image import ImageCaptcha [as 别名]
def gen_captcha(charset,nb_chars=None,font=None):
if not font is None:
image = ImageCaptcha(fonts=[font])
buffer_index=1000
buffer_size=1000
nc_set = np.zeros(buffer_size)
while True:
if buffer_index==buffer_size:
nc_set = np.random.randint(3, MAXLEN+1, buffer_size) if nb_chars is None else np.array([nb_chars] * buffer_size)
buffer_index=0
captcha_text = ''.join(random_chars(charset,nc_set[buffer_index]))
buffer_index+=1
img_text = ' '*np.random.randint(0,MAXLEN+1-len(captcha_text))*2+captcha_text #用空格模拟偏移
captcha = image.generate(img_text)
captcha_image = Image.open(captcha).resize((TARGET_WIDTH,TARGET_HEIGHT),Image.ANTIALIAS)
#image.write(captcha_text, captcha_text + '.jpg') # 写到文件
captcha_array = np.array(captcha_image)
yield captcha_array,captcha_text
示例3: __init__
# 需要导入模块: from captcha import image [as 别名]
# 或者: from captcha.image import ImageCaptcha [as 别名]
def __init__(self, img_width, img_height, ds_size, n_chars=4, chars=None):
self.gen = ImageCaptcha(img_width, img_height)
self.size = ds_size
self.n_chars = n_chars
if chars is None:
self.chars = list('1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
else:
self.chars = list(chars)
self.tokenizer = Tokenizer(self.chars)
self.first_run = True
示例4: __init__
# 需要导入模块: from captcha import image [as 别名]
# 或者: from captcha.image import ImageCaptcha [as 别名]
def __init__(self, h, w, font_paths):
"""
Parameters
----------
h: int
Height of the generated images
w: int
Width of the generated images
font_paths: list of str
List of all fonts in ttf format
"""
self.captcha = ImageCaptcha(fonts=font_paths)
self.h = h
self.w = w
示例5: main
# 需要导入模块: from captcha import image [as 别名]
# 或者: from captcha.image import ImageCaptcha [as 别名]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("font_path", help="Path to ttf font file")
parser.add_argument("output", help="Output filename including extension (e.g. 'sample.jpg')")
parser.add_argument("--num", help="Up to 4 digit number [Default: random]")
args = parser.parse_args()
captcha = ImageCaptcha(fonts=[args.font_path])
captcha_str = args.num if args.num else DigitCaptcha.get_rand(3, 4)
img = captcha.generate(captcha_str)
img = np.fromstring(img.getvalue(), dtype='uint8')
img = cv2.imdecode(img, cv2.IMREAD_GRAYSCALE)
cv2.imwrite(args.output, img)
print("Captcha image with digits {} written to {}".format([int(c) for c in captcha_str], args.output))
示例6: get_challenge
# 需要导入模块: from captcha import image [as 别名]
# 或者: from captcha.image import ImageCaptcha [as 别名]
def get_challenge(self, obj: models.Captcha):
# TODO Does this need to be stored in the object instance, in case this method gets called twice?
challenge = ImageCaptcha().generate(obj.content).getvalue()
return b64encode(challenge)
示例7: generate_captcha
# 需要导入模块: from captcha import image [as 别名]
# 或者: from captcha.image import ImageCaptcha [as 别名]
def generate_captcha(text='1'):
capt = ImageCaptcha(width=28, height=28, font_sizes=[24])
image = capt.generate_image(text)
image = np.array(image, dtype=np.uint8)
return image
示例8: gen_special_img
# 需要导入模块: from captcha import image [as 别名]
# 或者: from captcha.image import ImageCaptcha [as 别名]
def gen_special_img(text, file_path, width, height):
# 生成img文件
generator = ImageCaptcha(width=width, height=height) # 指定大小
img = generator.generate_image(text) # 生成图片
img.save(file_path) # 保存图片
示例9: gen_special_img
# 需要导入模块: from captcha import image [as 别名]
# 或者: from captcha.image import ImageCaptcha [as 别名]
def gen_special_img():
# 随机文字
text = ""
for j in range(char_count):
text += random.choice(characters)
print(text)
# 生成img文件
generator = ImageCaptcha(width=width, height=height) # 指定大小
img = generator.generate_image(text) # 生成图片
imgByteArr = io.BytesIO()
img.save(imgByteArr, format='PNG')
imgByteArr = imgByteArr.getvalue()
return imgByteArr
示例10: gen_captcha_text_image
# 需要导入模块: from captcha import image [as 别名]
# 或者: from captcha.image import ImageCaptcha [as 别名]
def gen_captcha_text_image(c_set):
"""
# 生成字符对应的验证码
"""
image = ImageCaptcha(width=120, height=60)
captcha_text = random_captcha_text(char_set=c_set)
captcha_text = ''.join(captcha_text)
captcha = image.generate(captcha_text)
captcha_image = Image.open(captcha)
captcha_image = np.array(captcha_image)
return captcha_text, captcha_image
示例11: test_image_generate
# 需要导入模块: from captcha import image [as 别名]
# 或者: from captcha.image import ImageCaptcha [as 别名]
def test_image_generate():
captcha = ImageCaptcha()
data = captcha.generate('1234')
assert hasattr(data, 'read')
captcha = WheezyCaptcha()
data = captcha.generate('1234')
assert hasattr(data, 'read')
示例12: gen
# 需要导入模块: from captcha import image [as 别名]
# 或者: from captcha.image import ImageCaptcha [as 别名]
def gen(gen_dir, total_size, chars_num):
if not os.path.exists(gen_dir):
os.makedirs(gen_dir)
image = ImageCaptcha(width=IMAGE_WIDTH, height=IMAGE_HEIGHT,font_sizes=[40])
# must be subset of config.CHAR_SETS
char_sets = 'ABCDEFGHIJKLMNPQRSTUVWXYZ'
for i in xrange(total_size):
label = ''.join(random.sample(char_sets, chars_num))
image.write(label, os.path.join(gen_dir, label+'_num'+str(i)+'.png'))