class ChineseIdiomCaptcha(ImageCaptcha): """A fixed-solution CAPTCHA that can be used to hide email addresses or URLs from bots""" fontFactory = Text.FontFactory(16, "cn/simyou.ttf") defaultSize = (80, 25) def __init__(self, solution=u'验证码', from_list=True): if from_list: idiom_chinese = Words.WordList("basic-english", minLength=5, maxLength=8) self.solution = idiom_chinese.pick() else: self.solution = solution super(ChineseIdiomCaptcha, self).__init__() def getLayers(self): self.addSolution(self.solution) textLayer = Text.TextLayer(self.solution, borderSize=2, fontFactory=self.fontFactory) return [ Backgrounds.CroppedImage(), textLayer, Distortions.SineWarp(amplitudeRange=(1, 1)), ]
class EnglishWordCaptcha(ImageCaptcha): """A fixed-solution CAPTCHA that can be used to hide email addresses or URLs from bots""" fontFactory = Text.FontFactory(16, "vera/Vera.ttf") defaultSize = (80, 25) def __init__(self, solution=u'验证码', from_list=True): if from_list: self.solution = Words.basic_english_restricted.pick() else: self.solution = solution super(EnglishWordCaptcha, self).__init__() def getLayers(self): self.addSolution(self.solution) textLayer = Text.TextLayer(self.solution, borderSize=1, fontFactory=self.fontFactory) return [ random.choice([Backgrounds.TiledImage()]), textLayer, Distortions.SineWarp(), ]
class NumberCaptcha(ImageCaptcha): """A fixed-solution CAPTCHA that can be used to hide email addresses or URLs from bots""" fontFactory = Text.FontFactory(16, "vera/Vera.ttf") defaultSize = (60, 24) def __init__(self, digits=4): if digits > 10: digits = 10 self.solution = ''.join( ['%s' % i for i in random.sample(range(0, 10), digits)]) super(NumberCaptcha, self).__init__() def getLayers(self): self.addSolution(self.solution) textLayer = Text.TextLayer(self.solution, borderSize=1, fontFactory=self.fontFactory) return [ # random.choice([ Backgrounds.TiledImage() ]), [ Backgrounds.SolidColor('white'), Backgrounds.RandomDots( colors=['yellow', 'blue', 'green', 'red'], numDots=10) ], textLayer, Distortions.SineWarp(), ]
def getLayers(self): word = Words.defaultWordList.pick() self.addSolution(word) return [ Backgrounds.SolidColor(), Text.TextLayer(word, fontFactory=Text.FontFactory((15, 15), "vera"), textColor='black', borderSize=0), Distortions.WarpBase(), ]
class RandCaptcha(ImageCaptcha): defaultSize = (120, 50) fontFactory = Text.FontFactory(18, "vera/VeraBd.ttf") def getLayers(self, solution="blah"): self.addSolution(solution) return ((Backgrounds.Grid(size=8, foreground="white"), Distortions.SineWarp(amplitudeRange=(5, 9))), (Text.TextLayer(solution, textColor='white', fontFactory=self.fontFactory), Distortions.SineWarp()))
class AntiSpam(BaseRender): fontFactory = Text.FontFactory(20, "vera/VeraBd.ttf") def get_layers(self, word): textLayer = Text.TextLayer(word, borderSize=1, fontFactory=self.fontFactory) return [ Backgrounds.CroppedImage(), textLayer, Distortions.SineWarp(amplitudeRange=(2, 4)), ]
def getLayers(self): word = WordList('basic-english').pick() self.addSolution(word) return [ random.choice( [Backgrounds.CroppedImage(), Backgrounds.TiledImage()]), Text.TextLayer(word, borderSize=1, fontFactory=Text.FontFactory(18, "vera/VeraBd.ttf")), Distortions.SineWarp() ]
class AntiSpam(ImageCaptcha): """A fixed-solution CAPTCHA that can be used to hide email addresses or URLs from bots""" fontFactory = Text.FontFactory(20, "vera/VeraBd.ttf") defaultSize = (512,50) def getLayers(self, solution="*****@*****.**"): self.addSolution(solution) textLayer = Text.TextLayer(solution, borderSize = 2, fontFactory = self.fontFactory) return [ Backgrounds.CroppedImage(), textLayer, Distortions.SineWarp(amplitudeRange = (2, 4)), ]
class BaseRender(object): defaultSize = (100, 30) defaultfontfactory = Text.FontFactory(20, "vera") def __init__(self): self.fontfactory = None self._word = None def render(self, word=None, size=None): if size is None: size = self.defaultSize if not word: word = self.word return word, self.__do_render(self.get_layers(word), Image.new("RGB", size)) def get_layers(self, word): return self.renderlist def set_font_factory(self, factory): self.fontfactory = factory def get_font_factory(self): if not self.fontfactory: return self.defaultfontfactory return self.fontfactory def __get_word(self): if not self._word: self._word = Words.defaultWordList.pick() return self._word word = property(__get_word) def __do_render(self, _list, img): for i in _list: if isinstance(i, (tuple, list)): img = self.__do_render(img) else: img = i.render(img) or img return img
def getLayers(self): word = self.word ff = Text.FontFactory(26, self.font) bg = random.choice([ Backgrounds.SolidColor(), Backgrounds.CroppedImage(), Backgrounds.TiledImage(), ]) bg = (bg, Backgrounds.Grid(), Distortions.SineWarp(amplitudeRange=(6, 10), periodRange=(0.1, 0.4))) layers = [ bg, ] if not self.easyMode: layers.append(Backgrounds.RandomDots()) layers.extend([ #Distortions.WigglyBlocks(), Text.TextLayer(word, borderSize=1, fontFactory=ff), Distortions.SineWarp() #Distortions.SineWarp() ]) return layers