2014-12-03 22 views
2

我感兴趣的是3D验证码的,我把它与单个字体的工作,如下图所示:创建具有不同的字体3D验证码

import string 
from matplotlib.font_manager import findSystemFonts 
import random 
from PIL import ImageFont, Image, ImageDraw 

def rand_font(fonts=[], fontpaths=None, fontext='ttf', font_encoding='', min_size=24, max_size=36): 
    if fonts == []: 
     fonts = findSystemFonts(fontpaths, fontext) 
    requested_font = fonts[random.randint(0, len(fonts)-1)] 
    font_size = random.randint(min_size, max_size) 
    return ImageFont.truetype(requested_font, font_size, encoding=font_encoding) 

def create_captcha(text): 
    def _rand_color(): 
     colors = ['red', 'orange', 'white', 'purple', 'green', 'yellow'] 
     return colors[random.randint(0, len(colors)-1)] 

    width = random.randint(400, 700) 
    height = random.randint(150, 200) 
    angle = angle if angle else uniform(-20, 20) 


    font = rand_font() 

    text_width, text_height = font.getsize(text) 

    img = Image.new("L", (text_width * 3, text_height * 3), "white") 
    draw = ImageDraw.Draw(img) 
    draw.text((text_width, text_height), text, font=font) 

    fig = pylab.figure(figsize=(width/100.0, height/100.0), dpi=4000) 
    axes = Axes3D(fig) 
    X, Y = numpy.meshgrid(range(img.size[0]), range(img.size[1])) 
    Z = 1 - numpy.asarray(img)/255 


    func = Axes3D.plot_surface if random.randint(0,1) == 0 else Axes3D.plot_wireframe 

    func(axes, X, -Y, Z, rstride=1, cstride=1, color=_rand_color()) 

    axes.set_zlim((-3, 3)) 
    axes.set_xlim((text_width * 1.1, text_width * 1.9)) 
    axes.set_ylim((-text_height * 1.9, -text_height* 1.1)) 
    axes.set_axis_off() 
    axes.view_init(elev=60, azim=-90) 

这就是好的,首先,它可以让我创建之类的东西这样的:
http://puu.sh/dfxcW/d9fc3f5c4e.jpg
http://puu.sh/dft1a/1d35f5c99a.png

我想,虽然做的是创建一个使用不同的字体和大小的每个字符验证码,并通过一个小的每个字符抵消y

由于地块关闭它基于numpy阵列我试图通过每个字符在文本中像这样的循环:

prev_x = 0 
x = [] 
y = [] 
z = [] 
for character in text: 
    X, Y = numpy.meshgrid(range(prev_x, prev_x + img.size[0]), range(img.size[1])) 
    for v in X.tolist(): 
     x.append(v) 
    for v in Y.tolist(): 
     y.append(v) 
    # same for z 
prev_x += 40 # trying to offset the characters by an x value so they dont overlap 
x = numpy.array(x) 
# same for y and z to convert back to numpy array 
Axes3D.plot_wireframe(x, -y, z, rstride=1, cstride=1) 

这导致shape mismatch: two or more arrays have incompatible dimensions on axis 1.这混淆了我,因为我觉得尺寸是准确的因为我正在每个人上进行相同的呼叫。我是新来的numpy和3D的东西,所以如果有人有任何建议,请让我知道!

+0

为什么不仔细检查尺寸?您在此跳过一些代码,因此无法准确调试。 – Ajean 2014-12-03 22:59:40

+0

我检查了尺寸,它们不匹配。我没有跳过代码,我提供了一切。第二部分可以很容易地复制到第一部分 – ZWiki 2014-12-04 15:27:01

+0

你肯定已经跳过代码 - 为了得到这个工作,我不得不添加大量的导入,并替换你说的“#same for z”的行,并且诸如此类的东西。另外你的'Axes3D.plot_wireframe()'调用应该是'axes.plot_wireframe()',所以除非你真的没有复制并粘贴真正的代码到问题中,否则其他的东西就完全搞砸了。鉴于您的问题标题与您的问题内容不符,我不确定您要查找的是什么。 – Ajean 2014-12-04 22:24:09

回答

2

在开始这件事的过程中,肯定有很多很酷的因素,它主要是在那里;这里是我为了获得随机字体和可能的颜色每个字符的基础上做出的mods。我确实改变了一些尺寸以适合我的屏幕。

from matplotlib.font_manager import findSystemFonts 
import random 
from PIL import ImageFont, Image, ImageDraw 
import numpy 
from mpl_toolkits.mplot3d.axes3d import Axes3D 
import matplotlib.pyplot as plt 

def rand_font(fonts=[], fontpaths=None, fontext='ttf', font_encoding='', min_size=24, max_size=36): 
    if fonts == []: 
     fonts = findSystemFonts(fontpaths, fontext) 
    requested_font = fonts[random.randint(0, len(fonts)-1)] 
    font_size = random.randint(min_size, max_size) 
    return ImageFont.truetype(requested_font, font_size, encoding=font_encoding) 

def create_captcha(text): 
    def _rand_color(): 
     colors = ['red', 'orange', 'purple', 'green', 'yellow'] 
     return colors[random.randint(0, len(colors)-1)] 

    # First font just gets the general offsets 
    font = rand_font() 
    text_width, text_height = font.getsize(text) 

    # Dont draw text here first 
    img = Image.new("L", (text_width * 3, text_height * 3), "white") 
    draw = ImageDraw.Draw(img) 

    fig = plt.figure(figsize=(12, 8)) 
    axes = Axes3D(fig) 

    # Do this way if you want random fonts AND colors 
    #================= 
    prev_x = 0 
    for character in text: 
     cfont = rand_font() 
     char_wid, char_height = cfont.getsize(character) 
     draw.text((prev_x+text_width, text_height), character, font=cfont) 

     X, Y = numpy.meshgrid(range(prev_x+text_width, prev_x+text_width+char_wid), 
           range(text_height, text_height+char_height)) 
     Z = 1 - numpy.asarray(img.crop((prev_x+text_width, text_height, 
             prev_x+text_width+char_wid, 
             text_height+char_height)))/255 
     axes.plot_wireframe(X, -Y, Z, rstride=1, cstride=1, color=_rand_color()) 

     prev_x += char_wid # trying to offset the characters by an x value so they dont overlap 
    #================= 

    # Do this way if you want just random fonts on each letter all one color 
    #================= 
    prev_x = 0 
    for character in text: 
     cfont = rand_font() 
     char_wid, char_height = cfont.getsize(character) 
     draw.text((prev_x+text_width, text_height), character, font=cfont) 

     prev_x += char_wid # trying to offset the characters by an x value so they dont overlap 

    X, Y = numpy.meshgrid(range(img.size[0]), range(img.size[1])) 
    Z = 1 - numpy.asarray(img)/255 
    axes.plot_wireframe(X, -Y, Z, rstride=1, cstride=1, color=_rand_color()) 
    #================= 

    axes.set_zlim((-3, 3)) 
    axes.set_xlim((text_width * 1.1, text_width * 1.9)) 
    axes.set_ylim((-text_height * 1.9, -text_height* 1.1)) 
    axes.set_axis_off() 
    axes.view_init(elev=60, azim=-90) 
    plt.show() 

create_captcha('TEST') 

的多字体的单色版本看起来是这样的: test image all one color

和多字体多色版本是这样的: test image multiple colors

这大概可以到制作看起来更好,如果你使它成为一个表面,并改变视角/高度来避免背景...也许是这样的: test image multiple colors with raised surface

至少应该是一个起点!