Agg缓冲区

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

Agg缓冲区

  1. import numpy as np
  2. from matplotlib.backends.backend_agg import FigureCanvasAgg
  3. import matplotlib.pyplot as plt
  4. plt.plot([1, 2, 3])
  5. canvas = plt.get_current_fig_manager().canvas
  6. agg = canvas.switch_backends(FigureCanvasAgg)
  7. agg.draw()
  8. s, (width, height) = agg.print_to_buffer()
  9. # Convert to a NumPy array.
  10. X = np.fromstring(s, np.uint8).reshape((height, width, 4))
  11. # Pass off to PIL.
  12. from PIL import Image
  13. im = Image.frombytes("RGBA", (width, height), s)
  14. # Uncomment this line to display the image using ImageMagick's `display` tool.
  15. # im.show()

下载这个示例