从C ++嵌入式解释器捕获python窗口输出

pt3dNyc

我正在使用boost python在C ++应用程序中嵌入python解释器。(pybind11也可以)

如果我从嵌入式解释器调用matplotlib,例如:

import matplotlib.pyplot as plt
import numpy as np
plt.plot([1,2,3,4],[1,4,9,16])
plt.show()

python解释器打开一个新窗口(与我的应用程序的主窗口分开)以显示matplotlib图。

我知道这是一个远景,但是有什么办法可以拦截呢?我希望能够捕获在此单独窗口中显示的像素,并在应用程序主窗口的图形上下文中显示它们。

我猜这是不可能的,因为我相信窗口是如何生成的。但想检查是否有人对此有任何见解。

gdlmx

您可以使用matplotlib硬拷贝后端之一,并将画布的像素保存到可以导出到C ++上下文的字符串以下是python代码:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot

fig = matplotlib.pyplot.figure()
ax  = fig.add_subplot ( 111 )

ax.plot([1,2,3,4],[1,4,9,16])

fig.canvas.draw ()

w,h = fig.canvas.get_width_height()  # width and height of the canvas
buf = fig.canvas.tostring_argb()     # a byte string of type uint8 

在C ++代码中,您可以使用变量whbuf在主窗口中显示该图。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章