Mayavi可以在透明背景下渲染人物场景吗? [英] Can Mayavi render a figure scene with a transparent background?

查看:190
本文介绍了Mayavi可以在透明背景下渲染人物场景吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mayavi.mlab生成网格图,并希望背景不透明度为0.(或透明).这可能吗?

I am generating a mesh plot using mayavi.mlab and want the background opacity to be 0. (or transparent). Is this possible?

推荐答案

如果您的目标是将mayavi图形集成到matplotlib图形中,则可以这样做.您可以使用mlab.screenshot获取numpy的RGBA值数组,并且mlab会将背景像素设置为自动具有alpha 0.然后,您可以通过imshow将此RGBA矩阵添加到matlab图形中.示例(摘自此处):

If your goal is to integrate the mayavi figure into a matplotlib figure, this is possible. You can use mlab.screenshot to get a numpy array of RGBA values, and mlab will set background pixels to have alpha 0 automatically. You can then add this RGBA matrix to a matlab figure via imshow. Example (adapted from here):

import numpy as np
from mayavi import mlab
import matplotlib.pyplot as plt
# set up some plotting params
dphi, dtheta = np.pi / 250.0, np.pi / 250.0
[phi, theta] = np.mgrid[0:np.pi + dphi * 1.5:dphi,
                        0:2 * np.pi + dtheta * 1.5:dtheta]
m0, m1, m2, m3 = 4, 3, 2, 3
m4, m5, m6, m7 = 6, 2, 6, 4
r = np.sin(m0 * phi) ** m1 + np.cos(m2 * phi) ** m3 + \
    np.sin(m4 * theta) ** m5 + np.cos(m6 * theta) ** m7
x = r * np.sin(phi) * np.cos(theta)
y = r * np.cos(phi)
z = r * np.sin(phi) * np.sin(theta)
# do the meshplot    
fig = mlab.figure(size=(480, 340))
mlab.mesh(x, y, z, colormap='cool', figure=fig)
imgmap = mlab.screenshot(figure=fig, mode='rgba', antialiased=True)
mlab.close(fig)
# do the matplotlib plot
fig2 = plt.figure(figsize=(7, 5))
plt.imshow(imgmap, zorder=4)
plt.plot(np.arange(0, 480), np.arange(480, 0, -1), 'r-')
plt.savefig('example.png')

您还可以使用plt.imsave(arr=imgmap, fname="foo.png")

这篇关于Mayavi可以在透明背景下渲染人物场景吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆