Example #1
0
def generateCaptcha():
    # First we generate a random string to use // Changed to only use lowercase letters, as by user requests
    chars = string.ascii_lowercase + string.digits
    text = ''.join(random.choice(chars) for x in range(5))

    # And generate the captcha
    captchaimage = ImageCaptcha()

    image = captchaimage.generate_image(text)

    # Now to add some noise
    captchaimage.create_noise_curve(image, image.getcolors())
    captchaimage.create_noise_dots(image, image.getcolors())

    # Now to write the file
    filenumber = 0
    while True:
        if os.path.exists("./img/captcha_" + str(filenumber) + ".png"):
            filenumber += 1
        else:
            break
    imagefile = "./img/captcha_" + str(filenumber) + ".png"
    captchaimage.write(text, imagefile)

    return imagefile, text
Example #2
0
def create_image_captcha(captcha_text):
    image_captcha = ImageCaptcha()
    image = image_captcha.generate_image(captcha_text)

    image_captcha.create_noise_curve(image, image.getcolors())

    image_captcha.create_noise_dots(image, image.getcolors())

    image_file = "image/captcha_" + captcha_text + ".png"
    image_captcha.write(captcha_text, image_file)
Example #3
0
def create_image_captcha(captcha_text):
    image_captcha = ImageCaptcha(width=400, height=150)
    image_data = image_captcha.generate_image(captcha_text)
    image_captcha.create_noise_curve(image_data, image_data.getcolors())
    image_captcha.create_noise_dots(image_data, image_data.getcolors())

    # image_file = "./captcha_"+captcha_text + ".png"
    # image_captcha.write(captcha_text, image_file)

    return image_data
def image_captcha(text):
    captcha = ImageCaptcha()
    image = captcha.generate_image(captcha_text)
    captcha.create_noise_curve(image, image.getcolors())
    captcha.create_noise_dots(image, image.getcolors())
    image_file = "./langscape" + '' + captcha_text + ".png"
    captcha.write(captcha_text, image_file)

    print(image_file + " has been created.")

    p.imshow(image)
    p.show()
Example #5
0
def create_image_captcha(captcha_text):
    c = Claptcha(captcha_text, "BLUELINES.ttf")
    text,image=c.image
    image_captcha = ImageCaptcha()
    image = image_captcha.generate_image(text)
    image_captcha.create_noise_curve(image, image.getcolors())
    image_captcha.create_noise_dots(image, image.getcolors())
    image_file = "./captcha_"+ ".png"
    image_captcha.write(captcha_text, image_file)
    img=cv2.imread(image_file)
    enc.CAPTHA_ENCRY.encry(img)
    cv2.imshow('out',img)
    print("done") 
Example #6
0
def createCaptchaImage(text):
    """ creating the captcha as per the given text """
    
    imageCaptcha = ImageCaptcha(width=300, height=100)
    captcha_text = text
    image = imageCaptcha.generate_image(captcha_text)
    imageCaptcha.create_noise_dots(image, image.getcolors())
    imageCaptcha.create_noise_curve(image, image.getcolors())

    image_file = captcha_text + ".png"
    imageCaptcha.write(captcha_text, image_file)

    return image_file
Example #7
0
def create_captcha_image(captcha_string):
    image_captcha = ImageCaptcha(font_sizes=[50])

    image = image_captcha.generate_image(captcha_string)

    image_captcha.create_noise_dots(image, image.getcolors())

    image_captcha.create_noise_curve(image, image.getcolors())

    image_file = "./color_generated_captcha_images/" + captcha_string + ".png"

    image_captcha.write(captcha_string, image_file)

    print(image_file + " has been created.")
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"
    image_captcha.write(captcha_text, image_file)
    return image_file
Example #9
0
def create_image_captcha(captcha_text):
    image_captcha = ImageCaptcha()
    image = image_captcha.generate_image(
        captcha_text
    )  # from captcha module calling function to create an image (blank image)
    image_captcha.create_noise_curve(
        image, image.getcolors())  # to create noise in the image
    image_captcha.create_noise_dots(
        image, image.getcolors())  # to create dot noise in the image
    image_file = "./captcha_" + captcha_text + ".png"  #saving the image as a png file
    image_captcha.write(
        captcha_text,
        image_file)  # writing the captcha text on to the generated image
    plt.imshow(image)  # matplotlib.pyplot moduleto display the image
    plt.show()
Example #10
0
def generate_img(char_num):
    global img_dir
    the_chars = gen_char(char_num)
    # 选择验证码的字体,图片大小,字体大小
    captcha = ImageCaptcha(fonts=['./font/simhei.ttf'])
    # 设置背景,颜色
    backgroud = random_color(238, 255)
    color = random_color(0, 200, opacity=None)
    # 创建图片
    img = captcha.create_captcha_image(the_chars, color, backgroud)
    captcha.create_noise_curve(img, color)
    captcha.create_noise_dots(img, color, number=None)
    from PIL import ImageFilter
    img.filter(ImageFilter.SMOOTH)
    # 保存生成的验证码,按照:序号_结果.png 保存
    img_name = the_chars + '.png'
    img_path = img_dir + '/' + img_name
    captcha.write(the_chars, img_path)
Example #11
0
    def capImage(self, 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"
        image_captcha.write(captcha_text, image_file)

        # Display the image in a matplotlib viewer.
        plt.imshow(image)
        plt.show()
def create_image_captcha(captcha_text):
    folder="./output/"
    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 = folder+"captcha_"+captcha_text + ".png"
    image_captcha.write(captcha_text, image_file)

    # Display the image in a matplotlib viewer.
    # plt.imshow(image)
    # plt.show()

    print(image_file + " has been created.")
Example #13
0
def base_capthca(lang):
    inline_kb_full = InlineKeyboardMarkup(row_width=4)
    # captcha_text_store = 'あかさたなはまやらわがざだばぴぢじぎりみひにちしきぃうぅくすつぬふむゆゅるぐずづぶぷぺべでぜげゑれめねてへせけぇえおこそとのほもよょろをごぞどぼぽ、ゞゝんっゔ'
    captcha_text_store = 'asdfghjkzxcvbnmqwertyu2345678'
    captcha_text = choices(captcha_text_store, k=8)
    btn_text = list(captcha_text)
    btn_pass = list(btn_text)
    btn_order = []
    btns = []
    for _ in range(8):
        random_index = randrange(len(btn_text))
        item = btn_text.pop(random_index)
        _id = f'btn_{item}'
        btn_order.append(btn_pass.index(item))
        btns.append(InlineKeyboardButton(str(item), callback_data=_id))
    inline_kb_full.row(*btns[0:4])
    inline_kb_full.row(*btns[4:8])
    backspace_btn = InlineKeyboardButton('⌫', callback_data='backspace')
    inline_kb_full.row(backspace_btn)

    # file = io.BytesIO()
    # image = Image.new('RGBA', size=(250, 50), color=(155, 0, 0))
    # d = ImageDraw.Draw(image)
    # d.text((10,10), "Hello World", fill=(255,255,0))
    # image.save(file, 'png')

    # image_captcha = ImageCaptcha(fonts=['/usr/share/fonts/truetype/fonts-japanese-gothic.ttf'],  width=460, height=200)
    # image_captcha = ImageCaptcha(fonts=['/home/albert/.local/share/fonts/Iosevka Term Nerd Font Complete.ttf'],  width=460, height=200)
    image_captcha = ImageCaptcha(fonts=['NotoSansCJKjp-Regular.otf'],
                                 width=350,
                                 height=200)
    image = image_captcha.generate_image(captcha_text)
    for _ in range(randint(1, 5)):
        image_captcha.create_noise_curve(image, image.getcolors())

    # Add noise dots for the image.
    image_captcha.create_noise_dots(image, image.getcolors())
    input_file = InputFile(image_captcha.generate(captcha_text))

    return (input_file, inline_kb_full, btn_pass)
Example #14
0
def gen_captcha():
    #定义图片对象
    generator = ImageCaptcha(width=90, height=35, font_sizes=[fontsize])
    #获取字符串
    captcha_text = random_captcha()
    #生成图像
    # captcha_img = Image.open(image.generate(captcha_text))
    # return captcha_text,captcha_img
    img = generator.create_captcha_image(captcha_text, getRandomColor(),
                                         (255, 255, 255))
    for i in range(0):
        img = generator.create_noise_curve(img, getRandomColor())
    captcha_img = generator.create_noise_dots(img, getRandomColor(), 0, 0)
    return captcha_text, captcha_img
Example #15
0
async def apiGetCaptcha(request: Request):
    s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    rs = ""
    for _ in range(6):
        rs = rs + random.choice(s)

    img = ImageCaptcha()
    image = img.generate_image(rs)
    rgbl = [
        random.randint(0, 255),
        random.randint(0, 255),
        random.randint(0, 255)
    ]
    image = img.create_noise_dots(image, tuple(rgbl))
    for _ in range(3):
        rgbl = [
            random.randint(0, 255),
            random.randint(0, 255),
            random.randint(0, 255)
        ]
        image = img.create_noise_curve(image, tuple(rgbl))

    f = BytesIO()
    image.save(f, format="jpeg")
    image_data = f.getvalue()
    f.close()

    b64 = base64.b64encode(image_data)

    conn = newconn()
    cur = conn.cursor()
    token = uuid.uuid4()
    cur.execute(
        f"INSERT INTO Captcha VALUES ('{token}', '{rs}', {int(time.time() + 30)})"
    )
    conn.commit()

    return {"success": True, "captchaToken": token, "captchaBase64": b64}
import random

list = [
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
    'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
    'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
    'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    'Y', 'Z'
]

# 定义验证码尺寸
width, height = 170, 80

#生成一万张验证码
for i in range(30000):
    generator = ImageCaptcha(width=width, height=height)

    # 从list中取出4个字符
    random_str = ''.join([random.choice(list) for j in range(4)])
    # 生成验证码
    img = generator.generate_image(random_str)
    # 在验证码上加干扰点
    generator.create_noise_dots(img, '#000000', 4, 40)

    # 在验证码上加干扰线
    generator.create_noise_curve(img, '#000000')

    # 将图片保存在目录yzm文件夹下
    file_name = './verification_code/' + random_str + '_' + str(i) + '.jpg'
    img.save(file_name)
from captcha.image import ImageCaptcha

image = ImageCaptcha(fonts=['NEON GLOW.otf'], width=140, height=60)
image.create_noise_dots = lambda x, a, b=None, c=None: x
image.create_noise_curve = lambda x, a: x

#data = image.generate('1234')
for i in range(0, 10):
    image.write('1234', str(i) + '.png')