Exemplo n.º 1
0
def showArray(*args):
   title,img =  _showArray(*args)
   if view is None:
       img.show()
   else:
       title = title.replace("/","_")
       fname = "/tmp/%s.png" % title.strip().replace(" ","_").lower()
       img.save(fname)
       view(fname)
Exemplo n.º 2
0
def showArrayGrad(title, image, theta, grad=None):
    from PIL import ImageDraw #@UnresolvedImport
    title,img = _showArray(title, image)
    img = img.convert("RGB")
    draw = ImageDraw.Draw(img)
    width, height = img.size
    if grad is None:
        grad = np.zeros_like(theta)
        grad[:,:] = 5
    else:
        grad = grad.copy()
        grad -= grad.min()
        grad /= grad.max()
        grad *= 10
    cos = np.cos(theta*3.14159)
    sin = np.sin(theta*3.14159)
    for x in range(10,width-10,5):
        for y in range(10,height-10,5):
            i,j  =y, x
            dx = grad[i,j] * sin[i,j]
            dy = grad[i,j] * cos[i,j]
            try:
                x1,y1 = int(x-dx), int(y-dy)
                x2,y2 = int(x+dx), int(y+dy)
            except ValueError:
                # Nans happen
                continue
            a,b,c = img.getpixel((x,y))
            grey = (a+b+c)/3
            if grey > 128:
                color="black"
            else:
                color = "white"
            draw.line([(x1,y1),(x2,y2)],fill=color)
    if view is None:
        img.show()
    else:
        fname = "/tmp/%s.png" % title.strip().replace(" ","").lower()
        img.save(fname)
        view(fname)