将图像转换为numpy数组
- 论坛
- 将图像转换为numpy数组
20 浏览
将图像转换为numpy数组
我正在尝试从Matplotlib图形中获取一个numpy数组图像,目前的做法是先保存到文件,然后再读取文件,但我觉得一定有更好的方法。以下是我目前的做法:
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure fig = Figure() canvas = FigureCanvas(fig) ax = fig.gca() ax.text(0.0,0.0,"Test", fontsize=45) ax.axis('off') canvas.print_figure("output.png") image = plt.imread("output.png")
我尝试了下面的方法:
image = np.fromstring( canvas.tostring_rgb(), dtype='uint8' )
这是我找到的一个示例,但它给出了一个错误,说'FigureCanvasAgg'对象没有属性'renderer'。