Pequeno script que cria um captcha visual, gerando números aleatoriamente. Tira partido do módulo PIL (Python Image Library) e do módulo random.
Módulo PIL - Python image library
Nota: O threshold varia a intensidade dos pontos brancos.
import Image, ImageDraw, ImageFont, random #settings number = str(2) random.seed() number2 = str(random.randint(0,100)) limit = 50 threshold = 50 #constants size = 360, 220 black = 0, 0, 0 white = 255, 255, 255 tmp = 0, 0 #random position x = random.randint(0, size[[0]]-110) y = random.randint(0, size[[1]]-100) #create objects im = Image.new('RGB', size, black) font = ImageFont.truetype("folks-bold.ttf", 13) font2 = ImageFont.truetype("verdanab.ttf", random.randint(95,100)) font3 = ImageFont.truetype("ariblk.ttf", random.randint(95,100)) font4 = ImageFont.truetype("verabd.ttf", random.randint(95,100)) draw = ImageDraw.Draw(im) #random black dots in the background for i in xrange(size[[0]]): for e in xrange(size[[1]]): if random.randint(0,threshold) == 0: draw.point((i,e), white) #write the text fontmask = font.getmask(number, mode="") fontmask.rotate(10) draw.text((size[[0]]-15, size[[1]]-20), number, font=font) for i in number2: rnd = random.randint(2,4) if rnd == 2: draw.text((x+tmp[[0]], y), i, font=font2) tmp = font2.getsize(i) elif rnd == 3: draw.text((x+tmp[[0]], y), i, font=font3) tmp = font3.getsize(i) else: draw.text((x+tmp[[0]], y), i, font=font4) tmp = font4.getsize(i) im.show()