python - 从数学绘图库窗口中提取图像 (jpg)
问题描述
我已经尝试找到一种方法将窗口内容从 matplotlib 直接复制到内存中 - 我想避免将其保存到中间 PNG 文件中,只是为了将其读回内存,如下所示。有什么建议吗?谢谢。
from matplotlib import pyplot as plt
# draw into window with detected objects bounding boxes
ax = utils.viz.plot_bbox(img, bbox, scores=scores, labels=labels,
thresh=ARGS.thresh, class_names=CLASSNAMES,
absolute_coordinates=False)
plt.show(block=False)
# capture contents of window to disk
plt.savefig ('out.png')
# read from disk for use down stream
img2 = cv2.imread('out.png')
# use img2 down stream
谢谢
解决方案
您可以使用fig.canvas.tostring_rgb
. 这是一些示例代码,评论中的详细信息。
# create a dummy image to plot
img = np.random.randint(255, size=(20,20,3))
# you need a figure to use canvas methods
# if you didn't create yours you can probably get it with plt.gcf()
fig, ax = plt.subplots()
# plot your stuff
ax.imshow(img)
# force draw
fig.canvas.draw()
# save buffer
w, h = fig.canvas.get_width_height()
buffer = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8).reshape(h, w, 3)
# display your plot
plt.show()
# look at the buffer
fig, ax = plt.subplots()
ax.imshow(buffer)
plt.show()
第一个情节
导出的缓冲区
推荐阅读
- python - 在 python 虚拟环境中 AWS S3 访问被拒绝
- batch-file - 如何使delphi VCL应用程序从命令行运行
- javascript - 不同表中的 Hilghligth 列和行
- javascript - 延迟加载多个嵌入 vimeo 视频
- javascript - 如何根据所选选项在数据库 MySQL 的文本框中显示数据?
- webrtc - UFW 端口转发不适用于我的 coturn 设置
- vscode-extensions - 从 Azure Data Studio 扩展中执行 SQL?
- java - 无法解析符号“响应”
- python - 不理解 OpenCV GitHub Wiki 的“TensorFlow 对象检测”
- c - 如何在stm32 cube IDE中的main函数之前开始调试