def _gen(): try: fp = tempfile.mkstemp(prefix='captcha')[1] r = random.Random() secret = '' tmpFont = ttfFont() for x in range(length): secret += str(r.randint(0, 9)) secret = 'abcd' c = Claptcha(secret, tmpFont, resample=Image.BICUBIC, noise=noise) c.write(fp) return secret, fp except Exception: return None, None
def generateCaptchaImage(): global CAPTCHA_TEXT c = Claptcha(generateRandomCaptcha(), 'captcha/FreeMono.ttf', resample=Image.BICUBIC, noise=0.3) CAPTCHA_TEXT, _ = c.write('captcha/static/captcha/captcha.png') return CAPTCHA_TEXT
def generate_claptcha_image(self, sequence): """ Receive a CAPTCHA sequence (string), creates a CAPTCHA image, and returns the generated file name. Using Calptcha to generate """ random_id = str(uuid.uuid4())[-8:] filename = "./{dirname}/{sequence}_{random_id}.png".format( dirname=self._base_dir_name, sequence=sequence, random_id=random_id) noise_level = round( random.uniform(0.0, 0.1), ndigits=3 ) # Randomly select a noise level for our generation, from a uniform distribution c = Claptcha(sequence, self.font, (self.img_w / 1.5, self.img_h / 1.5), noise=noise_level, resample=Image.BICUBIC) c.write(filename) return filename
def gen_Fix_Captcha(FOLDER, nb_pic): for _ in range(nb_pic): ''' Fixed length captcha''' c = Claptcha(rdFixString, randomFont(), (300, 100), resample=Image.BILINEAR, noise=randomNoise()) text, _ = c.write(FOLDER + 'temp.png') ''' print(text) ''' os.rename(FOLDER + 'temp.png', FOLDER + text + '.png')
def gen_Captcha(FOLDER, nb_pic): for _ in range(nb_pic): ''' Random length captcha''' c = Claptcha(rdString, FONT_PATH + '/Raleway-SemiBold.ttf', (300, 100), resample=Image.BILINEAR, noise=randomNoise()) text, _ = c.write(FOLDER + 'temp.png') ''' print(text) ''' os.rename(FOLDER + 'temp.png', FOLDER + text + '.png')
def gen_Captcha(FOLDER, nb_pic, img_w, img_h): for _ in range(nb_pic): ''' Random length captcha''' c = Claptcha(rdString, randomFont(), (img_w, img_h), resample=Image.BILINEAR, noise=0) if not os.path.exists(FOLDER): os.makedirs(FOLDER) text, _ = c.write(FOLDER + 'temp.png') ''' print(text) ''' os.rename(FOLDER + 'temp.png', FOLDER + text + '.png')
def captcha( ): #generate a captcha with random letters with a length between min_char and max_char min_char = 4 max_char = 7 allchar = string.ascii_letters + string.digits captchaWord = "".join( choice(allchar) for x in range(randint(min_char, max_char))).lower() # Initialize Claptcha object with "Text" as text and FreeMono as font c = Claptcha(captchaWord, "./Captcha/FreeMono.ttf") # Save a PNG file 'test.png' text, file = c.write('./cache/captcha.png') #print(text) # 'Text' thebugs are everywhere! #print(file) # 'test.png' debugging! return text, file
import random import string from claptcha import Claptcha def randomString(): rndLetters = (random.choice(string.ascii_uppercase) for _ in range(3)) return "".join(rndLetters) # Initialize Claptcha object with random text, FreeMono as font, of size # 100x30px, using bicubic resampling filter and adding a bit of white noise r_string = randomString() c = Claptcha(r_string, "FreeMono.ttf", (1000,500), resample=Image.BICUBIC, noise=0.3) text, _ = c.write('captcha1.png') img = Image.open('captcha1.png') img.show() print('Generated captcha : ',text) # 'PZTBXB', string printed into captcha1.png x1 = 0 y1 = 0 w1 = 0 z1 = 0 ready = 'a' while(ready != 'r'): ready = input("type r when u are ready : ") answer = ''
from claptcha import Claptcha c = Claptcha("2020", "2020.ttf", noise=0.4) c.write("2020.png")
from claptcha import Claptcha # Initialize Claptcha object with "Text" as text and FreeMono as font c = Claptcha("Text", "FreeMono.ttf") # Get PIL Image object text, image = c.image print(text) # 'Text' print(type(image)) # <class 'PIL.Image.Image'> # Get BytesIO object (note that it will represent a different image, just # with the same text) text, bytes = c.bytes print(text) # 'Text' print(type(bytes)) # <class '_io.BytesIO'> # Save a PNG file 'test.png' text, file = c.write('test.png') print(text) # 'Text' print(file) # 'test.png'
def generate(name = 'captcha.png'): # Initialize Claptcha object with random text c = Claptcha(randomMix, "Roboto-BlackItalic.ttf", resample=Image.BICUBIC, noise=0.3) text, _ = c.write(name) return (text) # string printed into captcha.png
# Global variables CharInCaptcha = 5 pathname = os.path.dirname(__file__) path = pathname + "\\images" #Main for _ in range(1): #Create Captcha and write to Images Folder contours2 = [] num_array = [] String = CaptchaFunctions.randomString(CharInCaptcha) StringName = (String + '.png') c = Claptcha(String, "FreeMono.ttf", noise=0.6) text, _ = c.write(os.path.join(path, StringName)) contours, hierarchy, returnImage = CaptchaFunctions.ImageCreate( StringName, path) CaptchaFunctions.elimContours(contours, contours2, hierarchy) #Exception checker if (CharInCaptcha < len(contours2)): os.remove(os.path.join(path, StringName)) continue try: for i in range(len(contours2)): (_, _, w, _) = cv2.boundingRect(contours2[i]) if (w > 40): os.remove(os.path.join(path, StringName))
def case3(): c = Claptcha("8069",'C:\\Windows\winsxs\\amd64_microsoft-windows-f..etype-timesnewroman_31bf3856ad364e35_6.1.7601.17514_none_3b958c66aff6cdb7\\times.ttf') t,_ = c.write('code1.png')
def Claptcha_get(char_text): c = Claptcha(char_text, 'C:\Windows\Fonts\ARIALUNI.TTF') captcha = c.write('./' + char_text + '.jpg') text, im = c.image return text, im
def randomString(): rndLetters = (random.choice(string.ascii_uppercase) for _ in range(6)) rd = "".join(rndLetters) c = Claptcha(rd, "newfont.ttf", (300, 150), resample=Image.BICUBIC) c.write('canvas.jpg')
if not os.path.exists(root_dir): os.makedirs(root_dir) train_path = os.path.join(root_dir, train_dir) test_path = os.path.join(root_dir, test_dir) os.makedirs(train_path) os.makedirs(test_path) annotation_file = \ open(os.path.join(root_dir, train_file_name), 'w') test_file = \ open(os.path.join(root_dir, test_file_name), 'w') img_path = '' for idx in range(int(num_samples)): txt = unique_nms(num_symbls, using_names) c = Claptcha(txt, font, (80, 32), margin=(2, 2), resample=Image.BICUBIC, noise=.3) if idx < (int(num_samples - num_test_samples) - 1): img_path = os.path.join(train_path, txt + '.png') annotation_file.write(txt + '.png') annotation_file.write('\n') else: img_path = os.path.join(test_path, txt + '.png') test_file.write(txt + '.png') test_file.write('\n') c.write(img_path)
# Get PIL Image object text, image = c.image print(text) # 'Text' print(type(image)) # <class 'PIL.Image.Image'> # Get BytesIO object (note that it will represent a different image, just # with the same text) text, bytes = c.bytes print(text) # 'Text' print(type(bytes)) # <class '_io.BytesIO'> # Save a PNG file 'test.png' text, file = c.write('test.png') print(text) # 'Text' print(file) # 'test.png' import random import string from PIL import Image from claptcha import Claptcha def randomString(): rndLetters = (random.choice(string.ascii_uppercase) for _ in range(6)) return "".join(rndLetters) # Initialize Claptcha object with random text, FreeMono as font, of size # 100x30px, using bicubic resampling filter and adding a bit of white noise c = Claptcha(randomString, "FreeMono.ttf", (100,30),
def generate_image_claptcha(text, filename): c = Claptcha(text, FONT, (WIDTH, HEIGHT)) c.write(filename)
import random import string from PIL import Image from claptcha import Claptcha def randomString(): rndLetters = (random.choice(string.ascii_uppercase) for _ in range(6)) return "".join(rndLetters) # Initialize Claptcha object with random text, FreeMono as font, of size # 100x30px, using bicubic resampling filter and adding a bit of white noise c = Claptcha(randomString, "FreeMonospaced-7ZXP.ttf", (100, 30), resample=Image.BICUBIC, noise=0.3) text, _ = c.write('captcha1.png') print(text) # 'PZTBXB', string printed into captcha1.png text, _ = c.write('captcha2.png') print(text) # 'NEDKEM', string printed into captcha2.png # Change images' size to 150x90 and estimated margin to 25x25 c.size = (150, 90) c.margin = (25, 25) text, _ = c.write('captcha3.png') print(text) # 'XCQYVS', captcha3.png has different dimentions than # captcha1.png and captcha2.png
from utils.utils import random_text from captcha.image import ImageCaptcha from glob import glob from PIL import Image from claptcha import Claptcha WIDTH = 250 HEIGHT = 80 IMAGES_PER_TEXT_LENGTH = 100 OUTPUT_DIR = ".\\captcha_images\\" FONTS = glob("C:\\Windows\\fonts\\*.ttf") CHARACTERS = [ x for x in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ] for font in FONTS: for len in range(5, 11): for i in range(IMAGES_PER_TEXT_LENGTH): captcha_text = random_text(CHARACTERS, len) claptcha = Claptcha(captcha_text, font, size=(WIDTH, HEIGHT), resample=Image.NEAREST, noise=0.8) claptcha.write(OUTPUT_DIR + captcha_text + ".jpg") captcha_text = random_text(CHARACTERS, len) image_captcha = ImageCaptcha(WIDTH, HEIGHT, [font]) image_captcha.generate_image(captcha_text) image_captcha.write(captcha_text, OUTPUT_DIR + captcha_text + ".jpg")
from claptcha import Claptcha font = "/Library/Fonts/AppleGothic.ttf" path = "./images/" for j in range(0, 10): for i in range(0, 100): c = Claptcha(str(j), font) c.size = (100, 100) c.margin = (5, 5) text, image = c.image text, bytes = c.bytes fileName = str(j) + "_" if i < 10: fileName = fileName + "00" + str(i) else: fileName = fileName + "0" + str(i) fileName = fileName + ".bmp" print(fileName) c.write(path + fileName)