简体   繁体   中英

Capturing python windowed output from C++ embedded interpreter

I am using boost python to embed a python interpreter in a C++ application. (pybind11 would also be fine)

If I call matplotlib from the embedded interpreter with something like:

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

The python interpreter opens a new window (separate from my application's primary window) to display the matplotlib plot.

I know it's a long shot, but is there any way to intercept this? I would like to be able to capture the pixels displayed in this separate window and display them within the graphics context of my application's primary window.

I'm guessing this is not possible due to how I believe the window is being generated. But wanted to check if anyone might have any insights on this.

You can make use of one of the hardcopy backends of matplotlib and save the pixels of the canvas to a string which can be exported to your C++ context . Below are the python codes:

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 

In your C++ code you can use variables w , h and buf to display the figure in your primary window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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