Mine værktøjer
Du er her: Forside diverse filer til download mm. captcha.py
Handlinger tilknyttet webside

captcha.py

af adminSidst opdateret 11/07 2007 11:47

Captcha i python

Klik her for at hente fil

Størrelse: 2.0 kB - Filtype text/python-source

Indhold af fil-indholdsobjekt

#!/usr/bin/python -u
# -*- coding: UTF-8 -*-
"""Example: This grabs a random word from the dictionary 'words' (one
    word per line) and generates a jpeg image named 'test.jpg'
"""
import cgi
import cgitb

cgitb.enable()

import random
import Image
import ImageFont
import ImageDraw
import ImageFilter

Page = """Content-type: text/html

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Se og hent kildekoden <a target="_blank" href="http://www.databassen.dk:8090/bauerdata/python-program-eksempler/captcha.py">her</a>
<hr>
<h3>CAPTCHA (Completely Automated Public Turing test to Tell Computers and Humans Apart)</h3>
<hr>
<a href="captcha.py"><img src="http://www.databassen.dk/captcha/test.jpg" /></a>
<hr>
Ved tryk på grafikken genereres en ny captcha
<hr>
<b>Python er nemt, Python er sjovt.</b>
</html>
"""
FONT_PATH = '/usr/X11R6/lib/X11/fonts/truetype/'
def gen_captcha( text="Static", fnt='verdana.ttf', fnt_sz=25, file_name="../htdocs/captcha/test.jpg", fmt='JPEG' ):
    """Generate a captcha image"""
    # randomly select the foreground color
    fgcolor = random.randint(0x000000,0x000fff)
    # make the background color the opposite of fgcolor"
    bgcolor = fgcolor ^ 0xffffff
    # create a font object
    font = ImageFont.truetype(FONT_PATH+fnt,fnt_sz)
    # determine dimensions of the text
    dim = font.getsize(text)
    # create a new image slightly larger that the text
    im = Image.new('RGB', (dim[0]+5,dim[1]+5), bgcolor)
    d = ImageDraw.Draw(im)
    x, y = im.size
    r = random.randint
    # draw 100 random colored boxes on the background
    for num in range(50):
        d.rectangle((r(0,x),r(0,y),r(0,x),r(0,y)),fill=r(0,0xffffff))
    # add the text to the image
    d.text((3,3), text, font=font, fill=fgcolor )
    im = im.filter( ImageFilter.EDGE_ENHANCE_MORE )
    # save the image to a file
    im.save( file_name, format=fmt )
    # return im

words = open( 'words' ).read().split()
word = random.choice( words )
gen_captcha( word )
print Page
Relateret indhold
« august 2018 »
søn man tir ons tor fre lør
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Chat Rooms
Dyrk motion

Trim ballerup

 

Powered by Plone, the Open Source Content Management System