繁体   English   中英

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

[英]Capturing python windowed output from C++ embedded interpreter

我正在使用 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 图。

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

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

您可以使用 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在主窗口中显示图形。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM