Add a default font

This commit is contained in:
Hsiaoming Yang
2014-11-27 13:55:45 +08:00
parent 1e6cf58fed
commit 37989ebfd1
3 changed files with 8 additions and 13 deletions

View File

@ -1,3 +1,4 @@
include LICENSE
include README.rst
recursive-include captcha/data *.wav
recursive-include captcha/data *.ttf

Binary file not shown.

View File

@ -6,6 +6,7 @@
Generate Image CAPTCHAs, just the normal image CAPTCHAs you are using.
"""
import os
import random
from PIL import Image
from PIL import ImageFilter
@ -17,9 +18,13 @@ try:
except ImportError:
wheezy_captcha = None
DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
DEFAULT_FONTS = [os.path.join(DATA_DIR, 'DroidSansMono.ttf')]
class _Captcha(object):
def generate(self, chars):
"""Generate an Image Captcha of the given characters."""
im = self.generate_image(chars)
out = BytesIO()
im.save(out, format='png')
@ -35,7 +40,7 @@ class WheezyCaptcha(_Captcha):
def __init__(self, width=200, height=75, fonts=None):
self._width = width
self._height = height
self._fonts = fonts
self._fonts = fonts or DEFAULT_FONTS
def generate_image(self, chars):
text_drawings = [
@ -66,7 +71,7 @@ class ImageCaptcha(_Captcha):
def __init__(self, width=160, height=60, fonts=None):
self._width = width
self._height = height
self._fonts = fonts
self._fonts = fonts or DEFAULT_FONTS
self._truefonts = []
@property
@ -169,17 +174,6 @@ class ImageCaptcha(_Captcha):
im = im.filter(ImageFilter.SMOOTH)
return im
def generate(self, chars):
im = self.generate_image(chars)
out = BytesIO()
im.save(out, format='png')
out.seek(0)
return out
def write(self, chars, output):
im = self.generate_image(chars)
return im.save(output, format='png')
def random_color(start, end, opacity=None):
red = random.randint(start, end)