Python captcha.image.ImageCaptcha() Examples

The following are 19 code examples of captcha.image.ImageCaptcha(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module captcha.image , or try the search function .
Example #1
Source File: captcha_generator.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
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)) 
Example #2
Source File: GenCaptcha.py    From CaptchaRecognition with MIT License 6 votes vote down vote up
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 
Example #3
Source File: captcha_generator.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
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)) 
Example #4
Source File: captcha_generator.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
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 
Example #5
Source File: captcha_generator.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
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 
Example #6
Source File: captcha_gen_default.py    From captcha_recognize with Apache License 2.0 5 votes vote down vote up
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')) 
Example #7
Source File: test_image.py    From captcha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_image_generate():
        captcha = ImageCaptcha()
        data = captcha.generate('1234')
        assert hasattr(data, 'read')

        captcha = WheezyCaptcha()
        data = captcha.generate('1234')
        assert hasattr(data, 'read') 
Example #8
Source File: tf_GAN.py    From deep_learning with MIT License 5 votes vote down vote up
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 
Example #9
Source File: webserver_captcha_image.py    From cnn_captcha with Apache License 2.0 5 votes vote down vote up
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 
Example #10
Source File: gen_sample_by_captcha.py    From cnn_captcha with Apache License 2.0 5 votes vote down vote up
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)  # 保存图片 
Example #11
Source File: evaluate.py    From slim_cnn_test with Apache License 2.0 5 votes vote down vote up
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 
Example #12
Source File: generate_train_data.py    From slim_cnn_test with Apache License 2.0 5 votes vote down vote up
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 
Example #13
Source File: captcha_test.py    From PyRoyale with GNU General Public License v3.0 5 votes vote down vote up
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. 
Example #14
Source File: captcha_generator.py    From training_results_v0.6 with Apache License 2.0 5 votes vote down vote up
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)) 
Example #15
Source File: captcha_generator.py    From training_results_v0.6 with Apache License 2.0 5 votes vote down vote up
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 
Example #16
Source File: dataset.py    From attention-ocr with MIT License 5 votes vote down vote up
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 
Example #17
Source File: evaluate.py    From multi_task_test with Apache License 2.0 5 votes vote down vote up
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 
Example #18
Source File: generate_train_data.py    From multi_task_test with Apache License 2.0 5 votes vote down vote up
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 
Example #19
Source File: serializers.py    From desec-stack with MIT License 5 votes vote down vote up
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)