음.... 추석날 파이선을 어떻게 하면서 만들어 본것.. orz

 

captcha.py

 

 

 

#!/usr/bin/python

import Image
import ImageDraw
import ImageFont
import ImageFilter
import ImageOps
import random

import sys
import string
from optparse import OptionParser
global ext
ext = ".jpg"

makeCnt = int(sys.argv[1])
 
def gen_captcha_img(strText):
 width = 280
 height = 100
 #Create the image
 im = Image.new("RGB", (width, height), "black")

 fontFile = "/usr/share/fonts/dejavu/DejaVuSans.ttf"
 rand = random.randint
 #Set the font and its size
 font = ImageFont.truetype(fontFile, 50)

 #Get image size
 x, y = im.size

 #Create an object that can be used to draw in the given image
 draw = ImageDraw.Draw(im)

 #Draw the text
 #fontColor = (rand(0,255), rand(0,255), rand(0,255))
 fontColor = "#fff"
 draw.text((35,25), strText, font=font, fill=fontColor)

 for i in range(15):
  draw.line((rand(0,x),rand(0,y), rand(0,x),rand(0,y)), fill=(rand(0,255), rand(0,255), rand(0,255)))

#   pxColor = "#fff"
 pxColor = (rand(0,255), rand(0,255), rand(0,255))
#   color += str(hex(rand(0,4095)))[2:5]
 #Draw points (individual pixels) inside the image area
 for i in range(300):
  draw.point((rand(0,x),rand(0,y),rand(0,x),rand(0,y)), fill=pxColor)

 #Apply a filter to the image
 im = im.filter(ImageFilter.BLUR)
 im = im.filter(ImageFilter.EDGE_ENHANCE_MORE)

 im.rotate(rand(0,90)-45).save(strText + ext)
#   im.save(strText + ext)


def gen_random_captcha(makeCnt):
 gen_tx = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()"
 textRand = random.randint
 textLen = 6
 for i in range(makeCnt):
  text = ""
  for j in range(textLen):
   text += gen_tx[textRand(0, len(gen_tx)-1)]

  gen_captcha_img(text)

gen_random_captcha(makeCnt)
print "made a captcha\n"

블로그 이미지

프로그래머 지향자 RosaGigantea

바쁜 일상 생활중의 기억 장소

Tag