# -*- coding: utf8 -*- #

import Image, ImageDraw, ImageFont
import random
from cStringIO import StringIO
import tornado
from Base import BaseHandler

class Captcha( BaseHandler ):
    def create_captcha( self, charNum, fontSize ):
        alphArr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
            'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T',
            'W', 'X', 'Z', '1', '2', '3', '4', '5', '6', '8']
        #fontfile = "%s/tools"
        font = ImageFont.truetype( '%s/sysdir/fonts/acidic.ttf' % self.homedir, fontSize )
        width, height = font.getsize( "W" )
        interval = width
        width, height = ( width + 7 ) * charNum, height + 20
        image = Image.new( "RGB", ( width, height ), ( 192, 192, 192 ) )
        draw = ImageDraw.Draw( image )
        captcha = ''
        for i in range( 0, charNum ):
            #создаем фон для буквы:
            charImg = Image.new( "RGB", ( fontSize + 10, fontSize + 10 ), "black" )
            tmpDraw = ImageDraw.Draw( charImg )
            #помещаем на фон случайную букву случайного цвета:
            buk = random.choice( alphArr )
            captcha = '%s%s' % ( captcha, buk )
            tmpDraw.text( ( 3, 1 ), buk, font = font,
              fill = ( random.randint( 20, 150 ), random.randint( 20, 140 ), random.randint( 160, 200 ) ) )
            charImg = charImg.rotate( random.randint( -30, 30 ) )
            #создаем маску, чтобы фон буквы стал прозрачным:
            mask = Image.new( 'L', ( fontSize + 10, fontSize + 10 ), 0 )
            mask.paste( charImg, ( 0, 0 ) )
            hpos = 10 + ( i * interval + random.randint( 10, interval - 10 ) )
            vpos = random.randint( 10, 20 )
            image.paste( charImg, ( hpos, vpos ), mask ) #два раза для лучшей видимости
            image.paste( charImg, ( hpos, vpos ), mask )

        for i in range( 0, random.randint( 4, 8 ) ):
            draw.line( 
                   ( random.randint( 6, width - 6 ),
                    random.randint( 3, height - 3 ),
                    random.randint( 6, width - 6 ),
                    random.randint( 2, height - 3 ) ),
                    fill = ( random.randint( 70, 150 ),
                            random.randint( 20, 220 ),
                            random.randint( 160, 220 ) ),
                    width = random.randint( 1, 2 ) )
        #image.save("captcha.jpg")
        out = StringIO()
        self.set_session( 'captcha', captcha )
        #import tools.webpy as web
        #web.setcookie('ss0', hashlib.md5(captcha).hexdigest(), 600)
        image.save( out, 'PNG' )
        return out.getvalue()

    def get( self ):
        self.set_header( 'Content-Type', 'image/png' )
        self.write( self.create_captcha( 4, 35 ) )

if __name__ == '__main__':
    Captcha.create_captcha( 7, 40 )

d978abbc0c95b55756f6d64b5ebeaa124b60507d
<mask token>


class Captcha(BaseHandler):
    <mask token>
    <mask token>


<mask token>

<mask token>


class Captcha(BaseHandler):

    def create_captcha(self, charNum, fontSize):
        alphArr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L',
            'M', 'N', 'O', 'P', 'R', 'S', 'T', 'W', 'X', 'Z', '1', '2', '3',
            '4', '5', '6', '8']
        font = ImageFont.truetype('%s/sysdir/fonts/acidic.ttf' % self.
            homedir, fontSize)
        width, height = font.getsize('W')
        interval = width
        width, height = (width + 7) * charNum, height + 20
        image = Image.new('RGB', (width, height), (192, 192, 192))
        draw = ImageDraw.Draw(image)
        captcha = ''
        for i in range(0, charNum):
            charImg = Image.new('RGB', (fontSize + 10, fontSize + 10), 'black')
            tmpDraw = ImageDraw.Draw(charImg)
            buk = random.choice(alphArr)
            captcha = '%s%s' % (captcha, buk)
            tmpDraw.text((3, 1), buk, font=font, fill=(random.randint(20, 
                150), random.randint(20, 140), random.randint(160, 200)))
            charImg = charImg.rotate(random.randint(-30, 30))
            mask = Image.new('L', (fontSize + 10, fontSize + 10), 0)
            mask.paste(charImg, (0, 0))
            hpos = 10 + (i * interval + random.randint(10, interval - 10))
            vpos = random.randint(10, 20)
            image.paste(charImg, (hpos, vpos), mask)
            image.paste(charImg, (hpos, vpos), mask)
        for i in range(0, random.randint(4, 8)):
            draw.line((random.randint(6, width - 6), random.randint(3, 
                height - 3), random.randint(6, width - 6), random.randint(2,
                height - 3)), fill=(random.randint(70, 150), random.randint
                (20, 220), random.randint(160, 220)), width=random.randint(
                1, 2))
        out = StringIO()
        self.set_session('captcha', captcha)
        image.save(out, 'PNG')
        return out.getvalue()

    def get(self):
        self.set_header('Content-Type', 'image/png')
        self.write(self.create_captcha(4, 35))


<mask token>

<mask token>


class Captcha(BaseHandler):

    def create_captcha(self, charNum, fontSize):
        alphArr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L',
            'M', 'N', 'O', 'P', 'R', 'S', 'T', 'W', 'X', 'Z', '1', '2', '3',
            '4', '5', '6', '8']
        font = ImageFont.truetype('%s/sysdir/fonts/acidic.ttf' % self.
            homedir, fontSize)
        width, height = font.getsize('W')
        interval = width
        width, height = (width + 7) * charNum, height + 20
        image = Image.new('RGB', (width, height), (192, 192, 192))
        draw = ImageDraw.Draw(image)
        captcha = ''
        for i in range(0, charNum):
            charImg = Image.new('RGB', (fontSize + 10, fontSize + 10), 'black')
            tmpDraw = ImageDraw.Draw(charImg)
            buk = random.choice(alphArr)
            captcha = '%s%s' % (captcha, buk)
            tmpDraw.text((3, 1), buk, font=font, fill=(random.randint(20, 
                150), random.randint(20, 140), random.randint(160, 200)))
            charImg = charImg.rotate(random.randint(-30, 30))
            mask = Image.new('L', (fontSize + 10, fontSize + 10), 0)
            mask.paste(charImg, (0, 0))
            hpos = 10 + (i * interval + random.randint(10, interval - 10))
            vpos = random.randint(10, 20)
            image.paste(charImg, (hpos, vpos), mask)
            image.paste(charImg, (hpos, vpos), mask)
        for i in range(0, random.randint(4, 8)):
            draw.line((random.randint(6, width - 6), random.randint(3, 
                height - 3), random.randint(6, width - 6), random.randint(2,
                height - 3)), fill=(random.randint(70, 150), random.randint
                (20, 220), random.randint(160, 220)), width=random.randint(
                1, 2))
        out = StringIO()
        self.set_session('captcha', captcha)
        image.save(out, 'PNG')
        return out.getvalue()

    def get(self):
        self.set_header('Content-Type', 'image/png')
        self.write(self.create_captcha(4, 35))


if __name__ == '__main__':
    Captcha.create_captcha(7, 40)

import Image, ImageDraw, ImageFont
import random
from cStringIO import StringIO
import tornado
from Base import BaseHandler


class Captcha(BaseHandler):

    def create_captcha(self, charNum, fontSize):
        alphArr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L',
            'M', 'N', 'O', 'P', 'R', 'S', 'T', 'W', 'X', 'Z', '1', '2', '3',
            '4', '5', '6', '8']
        font = ImageFont.truetype('%s/sysdir/fonts/acidic.ttf' % self.
            homedir, fontSize)
        width, height = font.getsize('W')
        interval = width
        width, height = (width + 7) * charNum, height + 20
        image = Image.new('RGB', (width, height), (192, 192, 192))
        draw = ImageDraw.Draw(image)
        captcha = ''
        for i in range(0, charNum):
            charImg = Image.new('RGB', (fontSize + 10, fontSize + 10), 'black')
            tmpDraw = ImageDraw.Draw(charImg)
            buk = random.choice(alphArr)
            captcha = '%s%s' % (captcha, buk)
            tmpDraw.text((3, 1), buk, font=font, fill=(random.randint(20, 
                150), random.randint(20, 140), random.randint(160, 200)))
            charImg = charImg.rotate(random.randint(-30, 30))
            mask = Image.new('L', (fontSize + 10, fontSize + 10), 0)
            mask.paste(charImg, (0, 0))
            hpos = 10 + (i * interval + random.randint(10, interval - 10))
            vpos = random.randint(10, 20)
            image.paste(charImg, (hpos, vpos), mask)
            image.paste(charImg, (hpos, vpos), mask)
        for i in range(0, random.randint(4, 8)):
            draw.line((random.randint(6, width - 6), random.randint(3, 
                height - 3), random.randint(6, width - 6), random.randint(2,
                height - 3)), fill=(random.randint(70, 150), random.randint
                (20, 220), random.randint(160, 220)), width=random.randint(
                1, 2))
        out = StringIO()
        self.set_session('captcha', captcha)
        image.save(out, 'PNG')
        return out.getvalue()

    def get(self):
        self.set_header('Content-Type', 'image/png')
        self.write(self.create_captcha(4, 35))


if __name__ == '__main__':
    Captcha.create_captcha(7, 40)

# -*- coding: utf8 -*- #

import Image, ImageDraw, ImageFont
import random
from cStringIO import StringIO
import tornado
from Base import BaseHandler

class Captcha( BaseHandler ):
    def create_captcha( self, charNum, fontSize ):
        alphArr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
            'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T',
            'W', 'X', 'Z', '1', '2', '3', '4', '5', '6', '8']
        #fontfile = "%s/tools"
        font = ImageFont.truetype( '%s/sysdir/fonts/acidic.ttf' % self.homedir, fontSize )
        width, height = font.getsize( "W" )
        interval = width
        width, height = ( width + 7 ) * charNum, height + 20
        image = Image.new( "RGB", ( width, height ), ( 192, 192, 192 ) )
        draw = ImageDraw.Draw( image )
        captcha = ''
        for i in range( 0, charNum ):
            #создаем фон для буквы:
            charImg = Image.new( "RGB", ( fontSize + 10, fontSize + 10 ), "black" )
            tmpDraw = ImageDraw.Draw( charImg )
            #помещаем на фон случайную букву случайного цвета:
            buk = random.choice( alphArr )
            captcha = '%s%s' % ( captcha, buk )
            tmpDraw.text( ( 3, 1 ), buk, font = font,
              fill = ( random.randint( 20, 150 ), random.randint( 20, 140 ), random.randint( 160, 200 ) ) )
            charImg = charImg.rotate( random.randint( -30, 30 ) )
            #создаем маску, чтобы фон буквы стал прозрачным:
            mask = Image.new( 'L', ( fontSize + 10, fontSize + 10 ), 0 )
            mask.paste( charImg, ( 0, 0 ) )
            hpos = 10 + ( i * interval + random.randint( 10, interval - 10 ) )
            vpos = random.randint( 10, 20 )
            image.paste( charImg, ( hpos, vpos ), mask ) #два раза для лучшей видимости
            image.paste( charImg, ( hpos, vpos ), mask )

        for i in range( 0, random.randint( 4, 8 ) ):
            draw.line( 
                   ( random.randint( 6, width - 6 ),
                    random.randint( 3, height - 3 ),
                    random.randint( 6, width - 6 ),
                    random.randint( 2, height - 3 ) ),
                    fill = ( random.randint( 70, 150 ),
                            random.randint( 20, 220 ),
                            random.randint( 160, 220 ) ),
                    width = random.randint( 1, 2 ) )
        #image.save("captcha.jpg")
        out = StringIO()
        self.set_session( 'captcha', captcha )
        #import tools.webpy as web
        #web.setcookie('ss0', hashlib.md5(captcha).hexdigest(), 600)
        image.save( out, 'PNG' )
        return out.getvalue()

    def get( self ):
        self.set_header( 'Content-Type', 'image/png' )
        self.write( self.create_captcha( 4, 35 ) )

if __name__ == '__main__':
    Captcha.create_captcha( 7, 40 )
