Agg缓冲区
使用后端AGG以RGB字符串的形式访问地物画布,然后将其转换为数组并将其传递给Pillow进行渲染。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import numpy as np
from matplotlib.backends.backend_agg import FigureCanvasAgg import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
canvas = plt.get_current_fig_manager().canvas
agg = canvas.switch_backends(FigureCanvasAgg) agg.draw() s, (width, height) = agg.print_to_buffer()
X = np.fromstring(s, np.uint8).reshape((height, width, 4))
from PIL import Image im = Image.frombytes("RGBA", (width, height), s)
|
下载这个示例