【问题标题】:Extract RGBA array from Figure matplotlib从图matplotlib中提取RGBA数组
【发布时间】:2017-05-08 01:41:47
【问题描述】:

我有以下代码可以创建音频文件的波形:

times = np.arange(len(data))/float(samplerate)
fig = plot.figure(figsize = (8,4), frameon = False)
axes = fig.gca()
if(channels == 1): #mono file
    axes.fill_between(times, data, color = 'k')
else: #stereo file
    axes.fill_between(times, data[:,0], data[:,1], color = 'k')

plot.savefig(savefile, dpi = 'figure', transparent = True)

我尝试了多种方法来从该图中提取 RGBA 数组,但都没有成功。我当前的代码如下:

canvas.draw()       # draw the canvas, cache the renderer

width, height = fig.get_size_inches() * fig.get_dpi()
waveformRGBA= np.fromstring(canvas.tostring_rgb(),dtype='uint8').reshape(height, width, 3)
print waveformRGBA

此方法生成 [255,255,255] 的空白 RGBA 数组

有什么想法吗???

【问题讨论】:

  • 您的“当前代码”未显示canvas 的创建方式。了解这可能会有所帮助。
  • 我看到您使用的命令几乎(但不完全)与上面链接的问题的答案相同。该答案中的示例对您也不起作用吗?否则尝试修复差异。

标签: python numpy matplotlib plot waveform


【解决方案1】:

从 png 文件中读取 RGB 和 alpha:

from scipy import misc
im = misc.imread("figure.png")

在这种情况下,im 是一个 3D 数组(行 x 列 x 4),其中包含尺寸为行 x 列的图形的每个像素的 RGB 和 alpha 信息。

【讨论】:

    猜你喜欢
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多