Convert Matplotlib figure to NumPy array without any borders/frame or axis

点点圈 提交于 2019-12-04 11:08:10

Well, it's not really an answer to the problem at hand using Matplotlib, but I gave up on this lib for this job and just used PIL.

It's quite easy, altough it's also quite slow (but I don't know if it's slower than Matplotlib).

The code is the following:

def makeImage (triangle, largura, altura):
    """
    triangle: receives a tuple in the form: x1, y1, x2, y2, x3, y3, R, G, B, A
    largura: image weight
    altura: image height

    returns: numPy array of the triangle composed final image
    """
    back = Image.new('RGBA', (largura,altura), (0,0,0,0))
    poly = Image.new('RGBA', (largura,altura))
    pdraw = ImageDraw.Draw(poly)

    pdraw.polygon([1,2,3,4,5,6], fill=(255,0,0,127))
    back.paste(poly,mask=poly)

    back = back.convert('RGB')
    backArr = asarray(back)
    #back.show()

    return backArr

If you know of a way to speed up this process, please do let me know.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!